Fix tests
Yuri Shkuro
6 years ago
25 | 25 | |
26 | 26 | instancer := NewInstancer(connection, appNameTest, loggerTest) |
27 | 27 | defer instancer.Stop() |
28 | ||
28 | 29 | endpointer := sd.NewEndpointer(instancer, factory, loggerTest) |
29 | ||
30 | 30 | endpoints, err := endpointer.Endpoints() |
31 | 31 | if err != nil { |
32 | 32 | t.Fatal(err) |
38 | 38 | } |
39 | 39 | |
40 | 40 | func TestInstancerScheduleUpdates(t *testing.T) { |
41 | factory := func(string) (endpoint.Endpoint, io.Closer, error) { | |
42 | return endpoint.Nop, nil, nil | |
43 | } | |
44 | ||
45 | 41 | connection := &testConnection{ |
46 | 42 | instances: []*fargo.Instance{instanceTest1}, |
47 | 43 | application: appUpdateTest, |
50 | 46 | |
51 | 47 | instancer := NewInstancer(connection, appNameTest, loggerTest) |
52 | 48 | defer instancer.Stop() |
53 | endpointer := sd.NewEndpointer(instancer, factory, loggerTest) | |
54 | 49 | |
55 | endpoints, _ := endpointer.Endpoints() | |
56 | if want, have := 1, len(endpoints); want != have { | |
50 | state := instancer.State() | |
51 | if want, have := 1, len(state.Instances); want != have { | |
57 | 52 | t.Errorf("want %d, have %d", want, have) |
58 | 53 | } |
59 | 54 | |
60 | 55 | time.Sleep(50 * time.Millisecond) |
61 | 56 | |
62 | endpoints, _ = endpointer.Endpoints() | |
63 | if want, have := 2, len(endpoints); want != have { | |
57 | state = instancer.State() | |
58 | if want, have := 2, len(state.Instances); want != have { | |
64 | 59 | t.Errorf("want %v, have %v", want, have) |
65 | 60 | } |
66 | 61 | } |
67 | 62 | |
68 | func TestBadFactory(t *testing.T) { | |
69 | factory := func(string) (endpoint.Endpoint, io.Closer, error) { | |
70 | return nil, nil, errTest | |
71 | } | |
72 | ||
73 | connection := &testConnection{ | |
74 | instances: []*fargo.Instance{instanceTest1}, | |
75 | application: appUpdateTest, | |
76 | errApplication: nil, | |
77 | } | |
78 | ||
79 | instancer := NewInstancer(connection, appNameTest, loggerTest) | |
80 | defer instancer.Stop() | |
81 | endpointer := sd.NewEndpointer(instancer, factory, loggerTest) | |
82 | ||
83 | endpoints, err := endpointer.Endpoints() | |
84 | if err != nil { | |
85 | t.Fatal(err) | |
86 | } | |
87 | ||
88 | if want, have := 0, len(endpoints); want != have { | |
89 | t.Errorf("want %d, have %d", want, have) | |
90 | } | |
91 | } | |
92 | ||
93 | 63 | func TestBadInstancerInstances(t *testing.T) { |
94 | factory := func(string) (endpoint.Endpoint, io.Closer, error) { | |
95 | return endpoint.Nop, nil, nil | |
96 | } | |
97 | ||
98 | 64 | connection := &testConnection{ |
99 | 65 | instances: []*fargo.Instance{}, |
100 | 66 | errInstances: errTest, |
104 | 70 | |
105 | 71 | instancer := NewInstancer(connection, appNameTest, loggerTest) |
106 | 72 | defer instancer.Stop() |
107 | endpointer := sd.NewEndpointer(instancer, factory, loggerTest) | |
108 | 73 | |
109 | endpoints, err := endpointer.Endpoints() | |
110 | if err != nil { | |
111 | t.Fatal(err) | |
74 | state := instancer.State() | |
75 | if state.Err == nil { | |
76 | t.Fatal("expecting error") | |
112 | 77 | } |
113 | 78 | |
114 | if want, have := 0, len(endpoints); want != have { | |
79 | if want, have := 0, len(state.Instances); want != have { | |
115 | 80 | t.Errorf("want %d, have %d", want, have) |
116 | 81 | } |
117 | 82 | } |
118 | 83 | |
119 | 84 | func TestBadInstancerScheduleUpdates(t *testing.T) { |
120 | factory := func(string) (endpoint.Endpoint, io.Closer, error) { | |
121 | return endpoint.Nop, nil, nil | |
122 | } | |
123 | ||
124 | 85 | connection := &testConnection{ |
125 | 86 | instances: []*fargo.Instance{instanceTest1}, |
126 | 87 | application: appUpdateTest, |
129 | 90 | |
130 | 91 | instancer := NewInstancer(connection, appNameTest, loggerTest) |
131 | 92 | defer instancer.Stop() |
132 | endpointer := sd.NewEndpointer(instancer, factory, loggerTest) | |
133 | 93 | |
134 | endpoints, err := endpointer.Endpoints() | |
135 | if err != nil { | |
136 | t.Error(err) | |
94 | state := instancer.State() | |
95 | if state.Err != nil { | |
96 | t.Error(state.Err) | |
137 | 97 | } |
138 | if want, have := 1, len(endpoints); want != have { | |
98 | if want, have := 1, len(state.Instances); want != have { | |
139 | 99 | t.Errorf("want %d, have %d", want, have) |
140 | 100 | } |
141 | 101 | |
142 | 102 | time.Sleep(50 * time.Millisecond) |
143 | 103 | |
144 | endpoints, err = endpointer.Endpoints() | |
145 | if err != nil { | |
146 | t.Error(err) | |
104 | state = instancer.State() | |
105 | if state.Err == nil { | |
106 | t.Fatal("expecting error") | |
147 | 107 | } |
148 | if want, have := 1, len(endpoints); want != have { | |
108 | ||
109 | if want, have := 0, len(state.Instances); want != have { | |
149 | 110 | t.Errorf("want %v, have %v", want, have) |
150 | 111 | } |
151 | 112 | } |