How to use Stub method of got Package

Best Got code snippet using got.Stub

Run Got automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

assume_role_provider_test.go

Source: assume_role_provider_test.go Github

copy
1package stscreds
2
3import (
4	"fmt"
5	"testing"
6	"time"
7
8	"github.com/aws/aws-sdk-go/aws"
9	"github.com/aws/aws-sdk-go/aws/credentials"
10	"github.com/aws/aws-sdk-go/aws/request"
11	"github.com/aws/aws-sdk-go/service/sts"
12)
13
14type stubSTS struct {
15	TestInput func(*sts.AssumeRoleInput)
16}
17
18func (s *stubSTS) AssumeRole(input *sts.AssumeRoleInput) (*sts.AssumeRoleOutput, error) {
19	if s.TestInput != nil {
20		s.TestInput(input)
21	}
22	expiry := time.Now().Add(60 * time.Minute)
23	return &sts.AssumeRoleOutput{
24		Credentials: &sts.Credentials{
25			// Just reflect the role arn to the provider.
26			AccessKeyId:     input.RoleArn,
27			SecretAccessKey: aws.String("assumedSecretAccessKey"),
28			SessionToken:    aws.String("assumedSessionToken"),
29			Expiration:      &expiry,
30		},
31	}, nil
32}
33
34type stubSTSWithContext struct {
35	stubSTS
36	called chan struct{}
37}
38
39func (s *stubSTSWithContext) AssumeRoleWithContext(context credentials.Context, input *sts.AssumeRoleInput, option ...request.Option) (*sts.AssumeRoleOutput, error) {
40	<-s.called
41	return s.stubSTS.AssumeRole(input)
42}
43
44func TestAssumeRoleProvider(t *testing.T) {
45	stub := &stubSTS{}
46	p := &AssumeRoleProvider{
47		Client:  stub,
48		RoleARN: "roleARN",
49	}
50
51	creds, err := p.Retrieve()
52	if err != nil {
53		t.Errorf("expect nil, got %v", err)
54	}
55
56	if e, a := "roleARN", creds.AccessKeyID; e != a {
57		t.Errorf("expect %v, got %v", e, a)
58	}
59	if e, a := "assumedSecretAccessKey", creds.SecretAccessKey; e != a {
60		t.Errorf("expect %v, got %v", e, a)
61	}
62	if e, a := "assumedSessionToken", creds.SessionToken; e != a {
63		t.Errorf("expect %v, got %v", e, a)
64	}
65}
66
67func TestAssumeRoleProvider_WithTokenCode(t *testing.T) {
68	stub := &stubSTS{
69		TestInput: func(in *sts.AssumeRoleInput) {
70			if e, a := "0123456789", *in.SerialNumber; e != a {
71				t.Errorf("expect %v, got %v", e, a)
72			}
73			if e, a := "code", *in.TokenCode; e != a {
74				t.Errorf("expect %v, got %v", e, a)
75			}
76		},
77	}
78	p := &AssumeRoleProvider{
79		Client:       stub,
80		RoleARN:      "roleARN",
81		SerialNumber: aws.String("0123456789"),
82		TokenCode:    aws.String("code"),
83	}
84
85	creds, err := p.Retrieve()
86	if err != nil {
87		t.Errorf("expect nil, got %v", err)
88	}
89
90	if e, a := "roleARN", creds.AccessKeyID; e != a {
91		t.Errorf("expect %v, got %v", e, a)
92	}
93	if e, a := "assumedSecretAccessKey", creds.SecretAccessKey; e != a {
94		t.Errorf("expect %v, got %v", e, a)
95	}
96	if e, a := "assumedSessionToken", creds.SessionToken; e != a {
97		t.Errorf("expect %v, got %v", e, a)
98	}
99}
100
101func TestAssumeRoleProvider_WithTokenProvider(t *testing.T) {
102	stub := &stubSTS{
103		TestInput: func(in *sts.AssumeRoleInput) {
104			if e, a := "0123456789", *in.SerialNumber; e != a {
105				t.Errorf("expect %v, got %v", e, a)
106			}
107			if e, a := "code", *in.TokenCode; e != a {
108				t.Errorf("expect %v, got %v", e, a)
109			}
110		},
111	}
112	p := &AssumeRoleProvider{
113		Client:       stub,
114		RoleARN:      "roleARN",
115		SerialNumber: aws.String("0123456789"),
116		TokenProvider: func() (string, error) {
117			return "code", nil
118		},
119	}
120
121	creds, err := p.Retrieve()
122	if err != nil {
123		t.Errorf("expect nil, got %v", err)
124	}
125
126	if e, a := "roleARN", creds.AccessKeyID; e != a {
127		t.Errorf("expect %v, got %v", e, a)
128	}
129	if e, a := "assumedSecretAccessKey", creds.SecretAccessKey; e != a {
130		t.Errorf("expect %v, got %v", e, a)
131	}
132	if e, a := "assumedSessionToken", creds.SessionToken; e != a {
133		t.Errorf("expect %v, got %v", e, a)
134	}
135}
136
137func TestAssumeRoleProvider_WithTokenProviderError(t *testing.T) {
138	stub := &stubSTS{
139		TestInput: func(in *sts.AssumeRoleInput) {
140			t.Errorf("API request should not of been called")
141		},
142	}
143	p := &AssumeRoleProvider{
144		Client:       stub,
145		RoleARN:      "roleARN",
146		SerialNumber: aws.String("0123456789"),
147		TokenProvider: func() (string, error) {
148			return "", fmt.Errorf("error occurred")
149		},
150	}
151
152	creds, err := p.Retrieve()
153	if err == nil {
154		t.Errorf("expect error")
155	}
156
157	if v := creds.AccessKeyID; len(v) != 0 {
158		t.Errorf("expect empty, got %v", v)
159	}
160	if v := creds.SecretAccessKey; len(v) != 0 {
161		t.Errorf("expect empty, got %v", v)
162	}
163	if v := creds.SessionToken; len(v) != 0 {
164		t.Errorf("expect empty, got %v", v)
165	}
166}
167
168func TestAssumeRoleProvider_MFAWithNoToken(t *testing.T) {
169	stub := &stubSTS{
170		TestInput: func(in *sts.AssumeRoleInput) {
171			t.Errorf("API request should not of been called")
172		},
173	}
174	p := &AssumeRoleProvider{
175		Client:       stub,
176		RoleARN:      "roleARN",
177		SerialNumber: aws.String("0123456789"),
178	}
179
180	creds, err := p.Retrieve()
181	if err == nil {
182		t.Errorf("expect error")
183	}
184
185	if v := creds.AccessKeyID; len(v) != 0 {
186		t.Errorf("expect empty, got %v", v)
187	}
188	if v := creds.SecretAccessKey; len(v) != 0 {
189		t.Errorf("expect empty, got %v", v)
190	}
191	if v := creds.SessionToken; len(v) != 0 {
192		t.Errorf("expect empty, got %v", v)
193	}
194}
195
196func BenchmarkAssumeRoleProvider(b *testing.B) {
197	stub := &stubSTS{}
198	p := &AssumeRoleProvider{
199		Client:  stub,
200		RoleARN: "roleARN",
201	}
202
203	b.ResetTimer()
204	for i := 0; i < b.N; i++ {
205		if _, err := p.Retrieve(); err != nil {
206			b.Fatal(err)
207		}
208	}
209}
210
211func TestAssumeRoleProvider_WithTags(t *testing.T) {
212	stub := &stubSTS{
213		TestInput: func(in *sts.AssumeRoleInput) {
214			if *in.TransitiveTagKeys[0] != "TagName" {
215				t.Errorf("TransitiveTagKeys not passed along")
216			}
217			if *in.Tags[0].Key != "TagName" || *in.Tags[0].Value != "TagValue" {
218				t.Errorf("Tags not passed along")
219			}
220		},
221	}
222	p := &AssumeRoleProvider{
223		Client:  stub,
224		RoleARN: "roleARN",
225		Tags: []*sts.Tag{
226			{
227				Key:   aws.String("TagName"),
228				Value: aws.String("TagValue"),
229			},
230		},
231		TransitiveTagKeys: []*string{aws.String("TagName")},
232	}
233	_, err := p.Retrieve()
234	if err != nil {
235		t.Errorf("expect error")
236	}
237}
238
239func TestAssumeRoleProvider_RetrieveWithContext(t *testing.T) {
240	stub := &stubSTSWithContext{
241		called: make(chan struct{}),
242	}
243	p := &AssumeRoleProvider{
244		Client:  stub,
245		RoleARN: "roleARN",
246	}
247
248	go func() {
249		stub.called <- struct{}{}
250	}()
251
252	creds, err := p.RetrieveWithContext(aws.BackgroundContext())
253	if err != nil {
254		t.Errorf("expect nil, got %v", err)
255	}
256
257	if e, a := "roleARN", creds.AccessKeyID; e != a {
258		t.Errorf("expect %v, got %v", e, a)
259	}
260	if e, a := "assumedSecretAccessKey", creds.SecretAccessKey; e != a {
261		t.Errorf("expect %v, got %v", e, a)
262	}
263	if e, a := "assumedSessionToken", creds.SessionToken; e != a {
264		t.Errorf("expect %v, got %v", e, a)
265	}
266}
267
Full Screen

move_test.go

Source: move_test.go Github

copy
1package main
2
3import (
4	"context"
5	"errors"
6	"fmt"
7	"github.com/google/go-cmp/cmp"
8	"reflect"
9	"testing"
10)
11
12type stub struct {
13	toFetch          []Thing
14	curr             int
15	fetchErr, putErr bool
16	gotPut           []Thing
17}
18
19func (s *stub) fetch() (Thing, bool) {
20	if s.curr >= len(s.toFetch) {
21		return nil, false
22	}
23
24	t := s.toFetch[s.curr]
25	s.curr++
26	return t, true
27}
28
29func (s *stub) put(t Thing) {
30	s.gotPut = append(s.gotPut, t)
31}
32
33func TestMoveCtx(t *testing.T) {
34	tests := []struct {
35		name string
36		stub *stub
37		want []Thing
38	}{
39		{
40			name: "empty",
41			stub: new(stub),
42			want: []Thing{},
43		},
44		{
45			name: "single",
46			stub: &stub{
47				toFetch: []Thing{1},
48			},
49			want: []Thing{1},
50		},
51		{
52			name: "multiple",
53			stub: &stub{
54				toFetch: []Thing{1, 2, 3},
55			},
56			want: []Thing{1, 2, 3},
57		},
58	}
59
60	for _, cancelFirst := range []bool{false, true} {
61		t.Run(fmt.Sprintf("Context cancelled %t", cancelFirst), func(t *testing.T) {
62			for _, tt := range tests {
63				t.Run(tt.name, func(t *testing.T) {
64
65					var wantErr error
66					var want []Thing
67
68					ctx, cancel := context.WithCancel(context.Background())
69					if cancelFirst {
70						cancel()
71						wantErr = context.Canceled
72					} else {
73						want = tt.want
74					}
75					defer cancel()
76
77					if err := MoveCtx(ctx, tt.stub.fetch, tt.stub.put); err != wantErr {
78						t.Errorf("MoveCtx() got error %v; want %v", err, wantErr)
79					}
80
81					got := tt.stub.gotPut
82
83					// This is because the playground can only use reflect.DeepEqual, which
84					// doesn't work properly for comparing nil and empty slices.
85					if len(want) == 0 {
86						if len(got) != 0 {
87							t.Errorf("Move() got values put %v; want empty", got)
88						}
89						return
90					}
91
92					if got, want := tt.stub.gotPut, tt.want; !reflect.DeepEqual(got, want) {
93						t.Errorf("MoveCtx() got values put %v; want %v", got, want)
94					}
95				})
96			}
97
98		})
99
100	}
101}
102
103// MaybeMove tests implementation
104func (s *stub) mayBeFetch() (Thing, bool, error) {
105	if s.fetchErr == true {
106		return nil, false, errors.New("something went wrong fetching things")
107	}
108
109	if s.curr >= len(s.toFetch) {
110		return nil, false, nil
111	}
112
113	t := s.toFetch[s.curr]
114	s.curr++
115	return t, true, nil
116}
117
118func (s *stub) mayBePut(t Thing) error {
119	if s.putErr {
120		return errors.New("could not continue putting thing")
121	}
122
123	s.gotPut = append(s.gotPut, t)
124	return nil
125}
126
127func TestMaybeMove(t *testing.T) {
128	t.Run("no errors during fetch and put", func(t *testing.T) {
129		testCase := struct {
130			stub *stub
131			want []Thing
132		}{
133			stub: &stub{
134				toFetch: []Thing{1, 2, 3},
135			},
136			want: []Thing{1, 2, 3},
137		}
138
139		if err := MaybeMove(testCase.stub.mayBeFetch, testCase.stub.mayBePut); err != nil {
140			t.Errorf("MaybeMove() got err %v; want %v", err, nil)
141		}
142
143		if diff := cmp.Diff(testCase.want, testCase.stub.gotPut); diff != "" {
144			t.Errorf("MaybeMove() mismatch (-want +got):\n%s", diff)
145		}
146	})
147
148	testCases := []struct{
149		name string
150		stub *stub
151		want []Thing
152	}{
153		{
154			name: "errors in put",
155			stub: &stub{
156				// I discovered a bug when []Thing{1, 2} is supplied, the second
157				// execution of fetch runs, why? It seems the second call to fetch was
158				// placed on the stack and executed leading to the panic on closing
159				// an already closed channel.
160				toFetch: []Thing{1, 2},
161				putErr:  true,
162			},
163		},
164		{
165			name: "errors in fetch",
166			stub: &stub{
167				toFetch: []Thing{1, 2},
168				fetchErr:  true,
169			},
170		},
171		{
172			name: "errors in fetch and put",
173			stub: &stub{
174				toFetch: []Thing{1, 2},
175				fetchErr:  true,
176				putErr: true,
177			},
178		},
179	}
180
181	for _, tc := range testCases {
182		t.Run(tc.name, func(t *testing.T) {
183			if err := MaybeMove(tc.stub.mayBeFetch, tc.stub.mayBePut); err == nil {
184				t.Errorf("MaybeMove() should get error, got %v", err)
185			}
186
187			if diff := cmp.Diff(tc.want, tc.stub.gotPut); diff != "" {
188				t.Errorf("MaybeMove() mismatch (-want +got):\n%s", diff)
189			}
190		})
191	}
192}
193
Full Screen

cmd_test.go

Source: cmd_test.go Github

copy
1package cmd
2
3import (
4	"errors"
5	"fmt"
6	"io"
7	"io/ioutil"
8	"net/http"
9	"net/http/httptest"
10	"reflect"
11	"strings"
12	"sync"
13	"testing"
14
15	"github.com/deis/deis/deisctl/backend"
16	"github.com/deis/deis/deisctl/config"
17	"github.com/deis/deis/deisctl/config/model"
18	"github.com/deis/deis/deisctl/test/mock"
19	"github.com/deis/deis/deisctl/units"
20)
21
22type backendStub struct {
23	startedUnits     []string
24	stoppedUnits     []string
25	installedUnits   []string
26	uninstalledUnits []string
27	restartedUnits   []string
28	expected         bool
29}
30
31func (backend *backendStub) Create(targets []string, wg *sync.WaitGroup, out, ew io.Writer) {
32	backend.installedUnits = append(backend.installedUnits, targets...)
33}
34func (backend *backendStub) Destroy(targets []string, wg *sync.WaitGroup, out, ew io.Writer) {
35	backend.uninstalledUnits = append(backend.uninstalledUnits, targets...)
36}
37func (backend *backendStub) Start(targets []string, wg *sync.WaitGroup, out, ew io.Writer) {
38	backend.startedUnits = append(backend.startedUnits, targets...)
39}
40func (backend *backendStub) Stop(targets []string, wg *sync.WaitGroup, out, ew io.Writer) {
41	backend.stoppedUnits = append(backend.stoppedUnits, targets...)
42}
43func (backend *backendStub) Scale(component string, num int, wg *sync.WaitGroup, out, ew io.Writer) {
44	switch {
45	case component == "router" && num == 3:
46		backend.expected = true
47	case component == "registry" && num == 4:
48		backend.expected = true
49	default:
50		backend.expected = false
51	}
52}
53func (backend *backendStub) RollingRestart(target string, wg *sync.WaitGroup, out, ew io.Writer) {
54	backend.restartedUnits = append(backend.restartedUnits, target)
55}
56
57func (backend *backendStub) ListMachines() error {
58	return nil
59}
60
61func (backend *backendStub) ListUnits() error {
62	return nil
63}
64func (backend *backendStub) ListUnitFiles() error {
65	return nil
66}
67func (backend *backendStub) Status(target string) error {
68	if target == "controller" || target == "builder" {
69		return nil
70	}
71	return errors.New("Test Error")
72}
73func (backend *backendStub) Journal(target string) error {
74	if target == "controller" || target == "builder" {
75		return nil
76	}
77	return errors.New("Test Error")
78}
79func (backend *backendStub) SSH(target string) error {
80	if target == "controller" {
81		return nil
82	}
83	return errors.New("Error")
84}
85func (backend *backendStub) SSHExec(target, command string) error {
86	if target == "controller" && command == "sh" {
87		return nil
88	}
89	return errors.New("Error")
90}
91
92func (backend *backendStub) Dock(target string, command []string) error {
93	return nil
94}
95
96var _ backend.Backend = &backendStub{}
97
98func fakeCheckKeys(cb config.Backend) error {
99	return nil
100}
101
102type fakeHTTPServer struct{}
103
104func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
105
106	if strings.Split(req.URL.Path, "/")[1] != "v1.7.2" {
107		res.WriteHeader(http.StatusNotFound)
108	}
109
110	res.Write([]byte("test"))
111}
112
113func TestRefreshUnits(t *testing.T) {
114	t.Parallel()
115
116	name, err := ioutil.TempDir("", "deisctl")
117
118	if err != nil {
119		t.Error(err)
120	}
121
122	handler := fakeHTTPServer{}
123	server := httptest.NewServer(handler)
124	defer server.Close()
125
126	err = RefreshUnits(name, "v1.7.2", server.URL+"/")
127
128	if err != nil {
129		t.Error(err)
130	}
131
132	files, err := ioutil.ReadDir(name)
133
134	// There will be a "decorators" subdirectory and that shouldn't be
135	// counted as a unit when making the upcoming assertion.
136	numFiles := len(files) - 1
137
138	if len(units.Names) != numFiles {
139		t.Error(fmt.Errorf("Expected %d units, Got %d", len(units.Names), numFiles))
140	}
141
142	for _, unit := range units.Names {
143		found := false
144
145		for _, file := range files {
146			if unit+".service" == file.Name() {
147				found = true
148			}
149		}
150
151		if found == false {
152			t.Error(fmt.Errorf("Expected to find %s in %v", unit, files))
153		}
154	}
155}
156
157func TestRefreshUnitsError(t *testing.T) {
158	t.Parallel()
159
160	name, err := ioutil.TempDir("", "deisctl")
161
162	if err != nil {
163		t.Error(err)
164	}
165
166	handler := fakeHTTPServer{}
167	server := httptest.NewServer(handler)
168	defer server.Close()
169
170	err = RefreshUnits(name, "foo", server.URL+"/")
171	result := err.Error()
172	expected := "404 Not Found"
173
174	if result != expected {
175		t.Error(fmt.Errorf("Expected %s, Got %s", expected, result))
176	}
177}
178
179func TestListUnits(t *testing.T) {
180	t.Parallel()
181
182	b := backendStub{installedUnits: []string{"[email protected]", "[email protected]"}}
183
184	if ListUnits(&b) != nil {
185		t.Error("unexpected error")
186	}
187}
188
189func TestListUnitFiles(t *testing.T) {
190	t.Parallel()
191
192	b := backendStub{}
193
194	if ListUnitFiles(&b) != nil {
195		t.Error("unexpected error")
196	}
197}
198
199func TestScaling(t *testing.T) {
200	t.Parallel()
201
202	b := backendStub{expected: false}
203	scale := []string{"registry=4", "router=3"}
204
205	Scale(scale, &b)
206
207	if b.expected == false {
208		t.Error("b.Scale called with unexpected arguements")
209	}
210}
211
212func TestScalingNonScalableComponent(t *testing.T) {
213	t.Parallel()
214
215	b := backendStub{}
216	expected := "cannot scale controller component"
217	err := Scale([]string{"controller=2"}, &b).Error()
218
219	if err != expected {
220		t.Error(fmt.Errorf("Expected '%v', Got '%v'", expected, err))
221	}
222}
223
224func TestScalingInvalidFormat(t *testing.T) {
225	t.Parallel()
226
227	b := backendStub{}
228	expected := "Could not parse: controller2"
229	err := Scale([]string{"controller2"}, &b).Error()
230
231	if err != expected {
232		t.Error(fmt.Errorf("Expected '%v', Got '%v'", expected, err))
233	}
234}
235
236func TestStart(t *testing.T) {
237	t.Parallel()
238
239	b := backendStub{}
240	expected := []string{"[email protected]", "[email protected]"}
241
242	Start(expected, &b)
243
244	if !reflect.DeepEqual(b.startedUnits, expected) {
245		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.startedUnits))
246	}
247}
248
249func TestStartPlatform(t *testing.T) {
250	t.Parallel()
251
252	b := backendStub{}
253	expected := []string{"store-monitor", "store-daemon", "store-metadata", "[email protected]*",
254		"store-volume", "logger", "logspout", "database", "[email protected]*", "controller",
255		"builder", "publisher", "[email protected]*", "database", "[email protected]*", "controller",
256		"builder", "publisher", "[email protected]*"}
257
258	Start([]string{"platform"}, &b)
259
260	if !reflect.DeepEqual(b.startedUnits, expected) {
261		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.startedUnits))
262	}
263}
264
265func TestStartStatelessPlatform(t *testing.T) {
266	t.Parallel()
267
268	b := backendStub{}
269	expected := []string{"logger", "logspout", "[email protected]*", "controller",
270		"builder", "publisher", "[email protected]*", "[email protected]*", "controller",
271		"builder", "publisher", "[email protected]*"}
272
273	Start([]string{"stateless-platform"}, &b)
274
275	if !reflect.DeepEqual(b.startedUnits, expected) {
276		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.startedUnits))
277	}
278}
279
280func TestStartSwarm(t *testing.T) {
281	t.Parallel()
282
283	b := backendStub{}
284	expected := []string{"swarm-manager", "swarm-node"}
285
286	Start([]string{"swarm"}, &b)
287
288	if !reflect.DeepEqual(b.startedUnits, expected) {
289		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.startedUnits))
290	}
291}
292
293func TestRollingRestart(t *testing.T) {
294	t.Parallel()
295
296	b := backendStub{}
297	expected := []string{"router"}
298
299	RollingRestart("router", &b)
300
301	if !reflect.DeepEqual(b.restartedUnits, expected) {
302		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.restartedUnits))
303	}
304}
305
306func TestUpgradePrep(t *testing.T) {
307	t.Parallel()
308
309	b := backendStub{}
310	expected := []string{"database", "[email protected]*", "controller", "builder", "logger", "logspout", "store-volume",
311		"[email protected]*", "store-metadata", "store-daemon", "store-monitor"}
312
313	UpgradePrep(false, &b)
314
315	if !reflect.DeepEqual(b.stoppedUnits, expected) {
316		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.stoppedUnits))
317	}
318}
319
320func TestStatelessUpgradePrep(t *testing.T) {
321	t.Parallel()
322
323	b := backendStub{}
324	expected := []string{"database", "[email protected]*", "controller", "builder", "logger", "logspout"}
325
326	UpgradePrep(true, &b)
327
328	if !reflect.DeepEqual(b.stoppedUnits, expected) {
329		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.stoppedUnits))
330	}
331}
332
333func TestUpgradeTakeover(t *testing.T) {
334	t.Parallel()
335	testMock := mock.ConfigBackend{Expected: []*model.ConfigNode{{Key: "/deis/services/app1", Value: "foo", TTL: 10},
336		{Key: "/deis/services/app2", Value: "8000", TTL: 10}}}
337
338	b := backendStub{}
339	expectedRestarted := []string{"router"}
340	expectedStarted := []string{"publisher", "store-monitor", "store-daemon", "store-metadata",
341		"[email protected]*", "store-volume", "logger", "logspout", "database", "[email protected]*",
342		"controller", "builder", "publisher", "database", "[email protected]*",
343		"controller", "builder", "publisher"}
344
345	if err := doUpgradeTakeOver(false, &b, testMock); err != nil {
346		t.Error(fmt.Errorf("Takeover failed: %v", err))
347	}
348
349	if !reflect.DeepEqual(b.restartedUnits, expectedRestarted) {
350		t.Error(fmt.Errorf("Expected %v, Got %v", expectedRestarted, b.restartedUnits))
351	}
352	if !reflect.DeepEqual(b.startedUnits, expectedStarted) {
353		t.Error(fmt.Errorf("Expected %v, Got %v", expectedStarted, b.startedUnits))
354	}
355}
356
357func TestStatelessUpgradeTakeover(t *testing.T) {
358	t.Parallel()
359	testMock := mock.ConfigBackend{Expected: []*model.ConfigNode{{Key: "/deis/services/app1", Value: "foo", TTL: 10},
360		{Key: "/deis/services/app2", Value: "8000", TTL: 10}}}
361
362	b := backendStub{}
363	expectedRestarted := []string{"router"}
364	expectedStarted := []string{"publisher", "logspout", "[email protected]*",
365		"controller", "builder", "publisher", "[email protected]*", "[email protected]*",
366		"controller", "builder", "publisher"}
367
368	if err := doUpgradeTakeOver(true, &b, testMock); err != nil {
369		t.Error(fmt.Errorf("Takeover failed: %v", err))
370	}
371
372	if !reflect.DeepEqual(b.restartedUnits, expectedRestarted) {
373		t.Error(fmt.Errorf("Expected %v, Got %v", expectedRestarted, b.restartedUnits))
374	}
375	if !reflect.DeepEqual(b.startedUnits, expectedStarted) {
376		t.Error(fmt.Errorf("Expected %v, Got %v", expectedStarted, b.startedUnits))
377	}
378}
379
380func TestStop(t *testing.T) {
381	t.Parallel()
382
383	b := backendStub{}
384	expected := []string{"[email protected]", "[email protected]"}
385	Stop(expected, &b)
386
387	if !reflect.DeepEqual(b.stoppedUnits, expected) {
388		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.stoppedUnits))
389	}
390}
391
392func TestStopPlatform(t *testing.T) {
393	t.Parallel()
394
395	b := backendStub{}
396	expected := []string{"[email protected]*", "publisher", "controller", "builder", "database",
397		"[email protected]*", "logger", "logspout", "store-volume", "[email protected]*",
398		"store-metadata", "store-daemon", "store-monitor"}
399	Stop([]string{"platform"}, &b)
400
401	if !reflect.DeepEqual(b.stoppedUnits, expected) {
402		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.stoppedUnits))
403	}
404}
405
406func TestStopStatelessPlatform(t *testing.T) {
407	t.Parallel()
408
409	b := backendStub{}
410	expected := []string{"[email protected]*", "publisher", "controller", "builder",
411		"[email protected]*", "logspout"}
412	Stop([]string{"stateless-platform"}, &b)
413
414	if !reflect.DeepEqual(b.stoppedUnits, expected) {
415		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.stoppedUnits))
416	}
417}
418
419func TestStopSwarm(t *testing.T) {
420	t.Parallel()
421
422	b := backendStub{}
423	expected := []string{"swarm-node", "swarm-manager"}
424	Stop([]string{"swarm"}, &b)
425
426	if !reflect.DeepEqual(b.stoppedUnits, expected) {
427		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.stoppedUnits))
428	}
429}
430
431func TestRestart(t *testing.T) {
432	t.Parallel()
433
434	b := backendStub{}
435	expected := []string{"[email protected]", "[email protected]"}
436
437	Restart(expected, &b)
438
439	if !reflect.DeepEqual(b.stoppedUnits, expected) {
440		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.stoppedUnits))
441	}
442	if !reflect.DeepEqual(b.startedUnits, expected) {
443		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.startedUnits))
444	}
445}
446
447func TestSSH(t *testing.T) {
448	t.Parallel()
449
450	b := backendStub{}
451	err := SSH("controller", []string{}, &b)
452
453	if err != nil {
454		t.Error(err)
455	}
456}
457func TestSSHExec(t *testing.T) {
458	t.Parallel()
459
460	b := backendStub{}
461	err := SSH("controller", []string{"sh"}, &b)
462
463	if err != nil {
464		t.Error(err)
465	}
466}
467
468func TestSSHError(t *testing.T) {
469	t.Parallel()
470
471	b := backendStub{}
472	err := SSH("registry", []string{}, &b)
473
474	if err == nil {
475		t.Error("Error expected")
476	}
477}
478
479func TestStatus(t *testing.T) {
480	t.Parallel()
481
482	b := backendStub{}
483
484	if Status([]string{"controller", "builder"}, &b) != nil {
485		t.Error("Unexpected Error")
486	}
487}
488
489func TestStatusError(t *testing.T) {
490	t.Parallel()
491
492	b := backendStub{}
493
494	expected := "Test Error"
495	err := Status([]string{"blah"}, &b).Error()
496
497	if err != expected {
498		t.Error(fmt.Errorf("Expected '%v', Got '%v'", expected, err))
499	}
500}
501
502func TestJournal(t *testing.T) {
503	t.Parallel()
504
505	b := backendStub{}
506
507	if Journal([]string{"controller", "builder"}, &b) != nil {
508		t.Error("Unexpected Error")
509	}
510}
511
512func TestJournalError(t *testing.T) {
513	t.Parallel()
514
515	b := backendStub{}
516
517	expected := "Test Error"
518	err := Journal([]string{"blah"}, &b).Error()
519
520	if err != expected {
521		t.Error(fmt.Errorf("Expected '%v', Got '%v'", expected, err))
522	}
523}
524
525func TestInstall(t *testing.T) {
526	t.Parallel()
527
528	b := backendStub{}
529	cb := mock.ConfigBackend{}
530
531	expected := []string{"[email protected]", "[email protected]"}
532
533	Install(expected, &b, &cb, fakeCheckKeys)
534
535	if !reflect.DeepEqual(b.installedUnits, expected) {
536		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.installedUnits))
537	}
538}
539
540func TestInstallPlatform(t *testing.T) {
541	t.Parallel()
542
543	b := backendStub{}
544	cb := mock.ConfigBackend{}
545
546	expected := []string{"store-daemon", "store-monitor", "store-metadata", "store-volume",
547		"[email protected]", "logger", "logspout", "database", "[email protected]",
548		"controller", "builder", "publisher", "[email protected]", "[email protected]", "[email protected]"}
549
550	Install([]string{"platform"}, &b, &cb, fakeCheckKeys)
551
552	if !reflect.DeepEqual(b.installedUnits, expected) {
553		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.installedUnits))
554	}
555}
556
557func TestInstallPlatformWithCustomRouterMeshSize(t *testing.T) {
558	t.Parallel()
559
560	b := backendStub{}
561	cb := mock.ConfigBackend{}
562
563	expected := []string{"store-daemon", "store-monitor", "store-metadata", "store-volume",
564		"[email protected]", "logger", "logspout", "database", "[email protected]",
565		"controller", "builder", "publisher", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"}
566	RouterMeshSize = 5
567
568	Install([]string{"platform"}, &b, &cb, fakeCheckKeys)
569	RouterMeshSize = DefaultRouterMeshSize
570
571	if !reflect.DeepEqual(b.installedUnits, expected) {
572		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.installedUnits))
573	}
574}
575
576func TestInstallStatelessPlatform(t *testing.T) {
577	t.Parallel()
578
579	b := backendStub{}
580	cb := mock.ConfigBackend{}
581
582	expected := []string{"logger", "logspout", "[email protected]",
583		"controller", "builder", "publisher", "[email protected]", "[email protected]", "[email protected]"}
584
585	Install([]string{"stateless-platform"}, &b, &cb, fakeCheckKeys)
586
587	if !reflect.DeepEqual(b.installedUnits, expected) {
588		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.installedUnits))
589	}
590}
591
592func TestInstallSwarm(t *testing.T) {
593	t.Parallel()
594
595	b := backendStub{}
596	cb := mock.ConfigBackend{}
597
598	expected := []string{"swarm-manager", "swarm-node"}
599
600	Install([]string{"swarm"}, &b, &cb, fakeCheckKeys)
601
602	if !reflect.DeepEqual(b.installedUnits, expected) {
603		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.installedUnits))
604	}
605}
606
607func TestUninstall(t *testing.T) {
608	t.Parallel()
609
610	b := backendStub{}
611	expected := []string{"[email protected]", "[email protected]"}
612
613	Uninstall(expected, &b)
614
615	if !reflect.DeepEqual(b.uninstalledUnits, expected) {
616		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.uninstalledUnits))
617	}
618}
619
620func TestUninstallPlatform(t *testing.T) {
621	t.Parallel()
622
623	b := backendStub{}
624	expected := []string{"[email protected]*", "publisher", "controller", "builder", "database",
625		"[email protected]*", "logger", "logspout", "store-volume", "[email protected]*",
626		"store-metadata", "store-daemon", "store-monitor"}
627
628	Uninstall([]string{"platform"}, &b)
629
630	if !reflect.DeepEqual(b.uninstalledUnits, expected) {
631		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.uninstalledUnits))
632	}
633}
634
635func TestUninstallStatelessPlatform(t *testing.T) {
636	t.Parallel()
637
638	b := backendStub{}
639	expected := []string{"[email protected]*", "publisher", "controller", "builder",
640		"[email protected]*", "logspout"}
641
642	Uninstall([]string{"stateless-platform"}, &b)
643
644	if !reflect.DeepEqual(b.uninstalledUnits, expected) {
645		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.uninstalledUnits))
646	}
647}
648
649func TestUninstallSwarm(t *testing.T) {
650	t.Parallel()
651
652	b := backendStub{}
653	expected := []string{"swarm-node", "swarm-manager"}
654
655	Uninstall([]string{"swarm"}, &b)
656
657	if !reflect.DeepEqual(b.uninstalledUnits, expected) {
658		t.Error(fmt.Errorf("Expected %v, Got %v", expected, b.uninstalledUnits))
659	}
660}
661
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)