示例#1
0
func TestApplyTemplate(t *testing.T) {
	cases := map[string]struct {
		stack string
		want  string
	}{
		"single guest stack": {
			"testdata/single-guest.json",
			"testdata/single-guest.json.golden",
		},
	}

	c := &stack.Credential{
		Credential: &softlayer.Credential{
			Username: os.Getenv("SL_USERNAME"),
			ApiKey:   os.Getenv("SL_API_KEY"),
		},
		Bootstrap: &softlayer.Bootstrap{
			KeyID: "12345",
		},
	}

	for name, cas := range cases {
		t.Run(name, func(t *testing.T) {
			pStack, err := ioutil.ReadFile(cas.stack)
			if err != nil {
				t.Fatal(err)
			}

			pWant, err := ioutil.ReadFile(cas.want)
			if err != nil {
				t.Fatal(err)
			}

			bs, _ := newBaseStack(string(pStack))
			s := &softlayer.Stack{BaseStack: bs}

			stack, err := s.ApplyTemplate(c)
			if err != nil {
				t.Fatal(err)
			}

			if err := providertest.Equal(stack.Content, string(pWant), stripNondeterministicResources); err != nil {
				t.Fatal(err)
			}
		})
	}
}
示例#2
0
func TestApplyTemplate(t *testing.T) {
	log := logging.NewCustom("test", true)

	cred := &stack.Credential{
		Identifier: "ident",
		Credential: &marathon.Credential{URL: "http://127.0.0.1:8080"},
	}

	cases := map[string]struct {
		stack  string
		want   string
		labels []marathon.Label
	}{
		"single app stack": {
			"testdata/single-app.json",
			"testdata/single-app.json.golden",
			[]marathon.Label{
				{Label: "/app", AppID: "/app-user-ident"},
			},
		},
		"multi app stack": {
			"testdata/multi-app.json",
			"testdata/multi-app.json.golden",
			[]marathon.Label{
				{Label: "/multi-app-1", AppID: "/multi-app/multi-app-user-ident-1"},
				{Label: "/multi-app-2", AppID: "/multi-app/multi-app-user-ident-2"},
				{Label: "/multi-app-3", AppID: "/multi-app/multi-app-user-ident-3"},
			},
		},
		"multi container stack": {
			"testdata/multi-container.json",
			"testdata/multi-container.json.golden",
			[]marathon.Label{
				{Label: "/multi-app-1", AppID: "/multi-app-user-ident"},
				{Label: "/multi-app-2", AppID: "/multi-app-user-ident"},
				{Label: "/multi-app-3", AppID: "/multi-app-user-ident"},
			},
		},
		"multi app multi container stack": {
			"testdata/multi-app-multi-container.json",
			"testdata/multi-app-multi-container.json.golden",
			[]marathon.Label{
				{Label: "/multi-app-1-1", AppID: "/multi-app/multi-app-user-ident-1"},
				{Label: "/multi-app-1-2", AppID: "/multi-app/multi-app-user-ident-1"},
				{Label: "/multi-app-1-3", AppID: "/multi-app/multi-app-user-ident-1"},
				{Label: "/multi-app-2-1", AppID: "/multi-app/multi-app-user-ident-2"},
				{Label: "/multi-app-2-2", AppID: "/multi-app/multi-app-user-ident-2"},
				{Label: "/multi-app-2-3", AppID: "/multi-app/multi-app-user-ident-2"},
				{Label: "/multi-app-3-1", AppID: "/multi-app/multi-app-user-ident-3"},
				{Label: "/multi-app-3-2", AppID: "/multi-app/multi-app-user-ident-3"},
				{Label: "/multi-app-3-3", AppID: "/multi-app/multi-app-user-ident-3"},
			},
		},
	}

	for name, cas := range cases {
		t.Run(name, func(t *testing.T) {
			pStack, err := ioutil.ReadFile(cas.stack)
			if err != nil {
				t.Fatalf("ReadFile()=%s", err)
			}

			pWant, err := ioutil.ReadFile(cas.want)
			if err != nil {
				t.Fatalf("ReadFile()=%s", err)
			}

			template, err := provider.ParseTemplate(string(pStack), log)
			if err != nil {
				t.Fatalf("ParseTemplate()=%s", err)
			}

			s := &marathon.Stack{
				BaseStack: &provider.BaseStack{
					Arg: &stack.ApplyRequest{
						GroupName: "foobar",
					},
					Session: &session.Session{
						Userdata: &userdata.Userdata{
							KlientURL: "http://127.0.0.1/klient.gz",
							Keycreator: &keycreator.Key{
								KontrolURL:        "http://127.0.0.1/kontrol/kite",
								KontrolPublicKey:  testkeys.Public,
								KontrolPrivateKey: testkeys.Private,
							},
						},
					},
					Builder: &provider.Builder{
						Template: template,
					},
					Req: &kite.Request{
						Username: "******",
					},
					KlientIDs: make(stack.KiteMap),
				},
				EntrypointBaseURL: "$ENTRYPOINT_URL",
				ScreenURL:         "$SCREEN_URL",
				CertURL:           "$CERT_URL",
				KlientURL:         "$KLIENT_URL",
			}

			stack, err := s.ApplyTemplate(cred)
			if err != nil {
				t.Fatalf("ApplyTemplate()=%s", err)
			}

			if err := providertest.Equal(stack.Content, string(pWant), stripNondeterministicResources); err != nil {
				t.Fatal(err)
			}

			if !reflect.DeepEqual(s.Labels, cas.labels) {
				t.Fatalf("got %#v, want %#v", s.Labels, cas.labels)
			}
		})
	}
}