<commit_before>// +build integration

package integration

import (
	"encoding/base64"
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"reflect"
	"strings"
	"testing"
	"time"

	"github.com/docker/distribution/registry/api/errcode"
	gocontext "golang.org/x/net/context"

	kapi "k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/api/errors"
	"k8s.io/kubernetes/pkg/api/unversioned"
	"k8s.io/kubernetes/pkg/client/restclient"
	kerrors "k8s.io/kubernetes/pkg/util/errors"
	"k8s.io/kubernetes/pkg/watch"

	"github.com/openshift/origin/pkg/dockerregistry"
	"github.com/openshift/origin/pkg/image/api"
	"github.com/openshift/origin/pkg/image/importer"
	testutil "github.com/openshift/origin/test/util"
	testserver "github.com/openshift/origin/test/util/server"

	"github.com/davecgh/go-spew/spew"
)

func TestImageStreamImport(t *testing.T) {
	testutil.RequireEtcd(t)
	_, clusterAdminKubeConfig, err := testserver.StartTestMaster()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	c, err := testutil.GetClusterAdminClient(clusterAdminKubeConfig)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	err = testutil.CreateNamespace(clusterAdminKubeConfig, testutil.Namespace())
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	// can't give invalid image specs, should be invalid
	isi, err := c.ImageStreams(testutil.Namespace()).Import(&api.ImageStreamImport{
		ObjectMeta: kapi.ObjectMeta{
			Name: "doesnotexist",
		},
		Spec: api.ImageStreamImportSpec{
			Images: []api.ImageImportSpec{
				{From: kapi.ObjectReference{Kind: "DockerImage", Name: "a/a/a/a/a/redis:latest"}, To: &kapi.LocalObjectReference{Name: "tag"}},
				{From: kapi.ObjectReference{Kind: "DockerImage", Name: "redis:latest"}},
			},
		},
	})
	if err == nil || isi != nil || !errors.IsInvalid(err) {
		t.Fatalf("unexpected responses: %#v %#v %#v", err, isi, isi.Status.Import)
	}
	// does not create stream
	if _, err := c.ImageStreams(testutil.Namespace()).Get("doesnotexist"); err == nil || !errors.IsNotFound(err) {
		t.Fatal(err)
	}

	// import without committing
	isi, err = c.ImageStreams(testutil.Namespace()).Import(&api.ImageStreamImport{
		ObjectMeta: kapi.ObjectMeta{
			Name: "doesnotexist",
		},
		Spec: api.ImageStreamImportSpec{
			Images: []api.ImageImportSpec{
				{From: kapi.ObjectReference{Kind: "DockerImage", Name: "redis:latest"}, To: &kapi.LocalObjectReference{Name: "other"}},
			},
		},
	})
	if err != nil || isi == nil || isi.Status.Import != nil {
		t.Fatalf("unexpected responses: %v %#v %#v", err, isi, isi.Status.Import)
	}
	// does not create stream
	if _, err := c.ImageStreams(testutil.Namespace()).Get("doesnotexist"); err == nil || !errors.IsNotFound(err) {
		t.Fatal(err)
	}

	// import with commit
	isi, err = c.ImageStreams(testutil.Namespace()).Import(&api.ImageStreamImport{
		ObjectMeta: kapi.ObjectMeta{
			Name: "doesnotexist",
		},
		Spec: api.ImageStreamImportSpec{
			Import: true,
			Images: []api.ImageImportSpec{
				{From: kapi.ObjectReference{Kind: "DockerImage", Name: "redis:latest"}, To: &kapi.LocalObjectReference{Name: "other"}},
			},
		},
	})
	if err != nil || isi == nil || isi.Status.Import == nil {
		t.Fatalf("unexpected responses: %v %#v %#v", err, isi, isi.Status.Import)
	}

	if isi.Status.Images[0].Image == nil || len(isi.Status.Images[0].Image.DockerImageLayers) == 0 {
		t.Fatalf("unexpected image output: %#v", isi.Status.Images[0].Image)
	}
	if isi.Status.Images[0].Image.DockerImageMetadata.Size == 0 {
		image, err := c.Images().Get(isi.Status.Images[0].Image.Name)
		if err != nil {
			t.Fatal(err)
		}
		if !strings.Contains(image.DockerImageManifest, `"schemaVersion": 1,`) {
			t.Fatalf("image has zero size, but schema version was not 1: %#v\n%s", isi.Status.Images[0].Image, image.DockerImageManifest)
		}
	}

	stream := isi.Status.Import
	if _, ok := stream.Annotations[api.DockerImageRepositoryCheckAnnotation]; !ok {
		t.Fatalf("unexpected stream: %#v", stream)
	}
	if stream.Generation != 1 || len(stream.Spec.Tags) != 1 || len(stream.Status.Tags) != 1 {
		t.Fatalf("unexpected stream: %#v", stream)
	}
	for tag, ref := range stream.Spec.Tags {
		if ref.Generation == nil || *ref.Generation != stream.Generation || tag != "other" || ref.From == nil ||
			ref.From.Name != "redis:latest" || ref.From.Kind != "DockerImage" {
			t.Fatalf("unexpected stream: %#v", stream)
		}
		event := stream.Status.Tags[tag]
		if len(event.Conditions) > 0 || len(event.Items) != 1 || event.Items[0].Generation != stream.Generation || strings.HasPrefix(event.Items[0].DockerImageReference, "docker.io/library/redis@sha256:") {<commit_msg>Revert "Allow size of image to be zero when schema1 from Hub"<commit_after>// +build integration

package integration

import (
	"encoding/base64"
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"reflect"
	"strings"
	"testing"
	"time"

	"github.com/docker/distribution/registry/api/errcode"
	gocontext "golang.org/x/net/context"

	kapi "k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/api/errors"
	"k8s.io/kubernetes/pkg/api/unversioned"
	"k8s.io/kubernetes/pkg/client/restclient"
	kerrors "k8s.io/kubernetes/pkg/util/errors"
	"k8s.io/kubernetes/pkg/watch"

	"github.com/openshift/origin/pkg/dockerregistry"
	"github.com/openshift/origin/pkg/image/api"
	"github.com/openshift/origin/pkg/image/importer"
	testutil "github.com/openshift/origin/test/util"
	testserver "github.com/openshift/origin/test/util/server"

	"github.com/davecgh/go-spew/spew"
)

func TestImageStreamImport(t *testing.T) {
	testutil.RequireEtcd(t)
	_, clusterAdminKubeConfig, err := testserver.StartTestMaster()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	c, err := testutil.GetClusterAdminClient(clusterAdminKubeConfig)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	err = testutil.CreateNamespace(clusterAdminKubeConfig, testutil.Namespace())
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	// can't give invalid image specs, should be invalid
	isi, err := c.ImageStreams(testutil.Namespace()).Import(&api.ImageStreamImport{
		ObjectMeta: kapi.ObjectMeta{
			Name: "doesnotexist",
		},
		Spec: api.ImageStreamImportSpec{
			Images: []api.ImageImportSpec{
				{From: kapi.ObjectReference{Kind: "DockerImage", Name: "a/a/a/a/a/redis:latest"}, To: &kapi.LocalObjectReference{Name: "tag"}},
				{From: kapi.ObjectReference{Kind: "DockerImage", Name: "redis:latest"}},
			},
		},
	})
	if err == nil || isi != nil || !errors.IsInvalid(err) {
		t.Fatalf("unexpected responses: %#v %#v %#v", err, isi, isi.Status.Import)
	}
	// does not create stream
	if _, err := c.ImageStreams(testutil.Namespace()).Get("doesnotexist"); err == nil || !errors.IsNotFound(err) {
		t.Fatal(err)
	}

	// import without committing
	isi, err = c.ImageStreams(testutil.Namespace()).Import(&api.ImageStreamImport{
		ObjectMeta: kapi.ObjectMeta{
			Name: "doesnotexist",
		},
		Spec: api.ImageStreamImportSpec{
			Images: []api.ImageImportSpec{
				{From: kapi.ObjectReference{Kind: "DockerImage", Name: "redis:latest"}, To: &kapi.LocalObjectReference{Name: "other"}},
			},
		},
	})
	if err != nil || isi == nil || isi.Status.Import != nil {
		t.Fatalf("unexpected responses: %v %#v %#v", err, isi, isi.Status.Import)
	}
	// does not create stream
	if _, err := c.ImageStreams(testutil.Namespace()).Get("doesnotexist"); err == nil || !errors.IsNotFound(err) {
		t.Fatal(err)
	}

	// import with commit
	isi, err = c.ImageStreams(testutil.Namespace()).Import(&api.ImageStreamImport{
		ObjectMeta: kapi.ObjectMeta{
			Name: "doesnotexist",
		},
		Spec: api.ImageStreamImportSpec{
			Import: true,
			Images: []api.ImageImportSpec{
				{From: kapi.ObjectReference{Kind: "DockerImage", Name: "redis:latest"}, To: &kapi.LocalObjectReference{Name: "other"}},
			},
		},
	})
	if err != nil || isi == nil || isi.Status.Import == nil {
		t.Fatalf("unexpected responses: %v %#v %#v", err, isi, isi.Status.Import)
	}

	if isi.Status.Images[0].Image == nil || isi.Status.Images[0].Image.DockerImageMetadata.Size == 0 || len(isi.Status.Images[0].Image.DockerImageLayers) == 0 {
		t.Fatalf("unexpected image output: %#v", isi.Status.Images[0].Image)
	}

	stream := isi.Status.Import
	if _, ok := stream.Annotations[api.DockerImageRepositoryCheckAnnotation]; !ok {
		t.Fatalf("unexpected stream: %#v", stream)
	}
	if stream.Generation != 1 || len(stream.Spec.Tags) != 1 || len(stream.Status.Tags) != 1 {
		t.Fatalf("unexpected stream: %#v", stream)
	}
	for tag, ref := range stream.Spec.Tags {
		if ref.Generation == nil || *ref.Generation != stream.Generation || tag != "other" || ref.From == nil ||
			ref.From.Name != "redis:latest" || ref.From.Kind != "DockerImage" {
			t.Fatalf("unexpected stream: %#v", stream)
		}
		event := stream.Status.Tags[tag]
		if len(event.Conditions) > 0 || len(event.Items) != 1 || event.Items[0].Generation != stream.Generation || strings.HasPrefix(event.Items[0].DockerImageReference, "docker.io/library/redis@sha256:") {
{'fraction_non_alphanumeric': 0.17542486583184258, 'fraction_numerical': 0.005925760286225402, 'mean_word_length': 4.511398644485521, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2257535', 'n_tokens_mistral': 3464, 'n_tokens_neox': 3258, 'n_words': 592}