github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/command/helpers_test.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "io" 6 "io/ioutil" 7 "net/http" 8 "os" 9 "reflect" 10 "strings" 11 "testing" 12 "time" 13 14 "github.com/mitchellh/cli" 15 ) 16 17 func TestHelpers_FormatKV(t *testing.T) { 18 in := []string{"alpha|beta", "charlie|delta", "echo|"} 19 out := formatKV(in) 20 21 expect := "alpha = beta\n" 22 expect += "charlie = delta\n" 23 expect += "echo = <none>" 24 25 if out != expect { 26 t.Fatalf("expect: %s, got: %s", expect, out) 27 } 28 } 29 30 func TestHelpers_FormatList(t *testing.T) { 31 in := []string{"alpha|beta||delta"} 32 out := formatList(in) 33 34 expect := "alpha beta <none> delta" 35 36 if out != expect { 37 t.Fatalf("expect: %s, got: %s", expect, out) 38 } 39 } 40 41 func TestHelpers_NodeID(t *testing.T) { 42 srv, _, _ := testServer(t, nil) 43 defer srv.Stop() 44 45 meta := Meta{Ui: new(cli.MockUi)} 46 client, err := meta.Client() 47 if err != nil { 48 t.FailNow() 49 } 50 51 // This is because there is no client 52 if _, err := getLocalNodeID(client); err == nil { 53 t.Fatalf("getLocalNodeID() should fail") 54 } 55 } 56 57 func TestHelpers_LineLimitReader_NoTimeLimit(t *testing.T) { 58 helloString := `hello 59 world 60 this 61 is 62 a 63 test` 64 65 noLines := "jskdfhjasdhfjkajkldsfdlsjkahfkjdsafa" 66 67 cases := []struct { 68 Input string 69 Output string 70 Lines int 71 SearchLimit int 72 }{ 73 { 74 Input: helloString, 75 Output: helloString, 76 Lines: 6, 77 SearchLimit: 1000, 78 }, 79 { 80 Input: helloString, 81 Output: `world 82 this 83 is 84 a 85 test`, 86 Lines: 5, 87 SearchLimit: 1000, 88 }, 89 { 90 Input: helloString, 91 Output: `test`, 92 Lines: 1, 93 SearchLimit: 1000, 94 }, 95 { 96 Input: helloString, 97 Output: "", 98 Lines: 0, 99 SearchLimit: 1000, 100 }, 101 { 102 Input: helloString, 103 Output: helloString, 104 Lines: 6, 105 SearchLimit: 1, // Exceed the limit 106 }, 107 { 108 Input: noLines, 109 Output: noLines, 110 Lines: 10, 111 SearchLimit: 1000, 112 }, 113 { 114 Input: noLines, 115 Output: noLines, 116 Lines: 10, 117 SearchLimit: 2, 118 }, 119 } 120 121 for i, c := range cases { 122 in := ioutil.NopCloser(strings.NewReader(c.Input)) 123 limit := NewLineLimitReader(in, c.Lines, c.SearchLimit, 0) 124 outBytes, err := ioutil.ReadAll(limit) 125 if err != nil { 126 t.Fatalf("case %d failed: %v", i, err) 127 } 128 129 out := string(outBytes) 130 if out != c.Output { 131 t.Fatalf("case %d: got %q; want %q", i, out, c.Output) 132 } 133 } 134 } 135 136 type testReadCloser struct { 137 data chan []byte 138 } 139 140 func (t *testReadCloser) Read(p []byte) (n int, err error) { 141 select { 142 case b, ok := <-t.data: 143 if !ok { 144 return 0, io.EOF 145 } 146 147 return copy(p, b), nil 148 case <-time.After(10 * time.Millisecond): 149 return 0, nil 150 } 151 } 152 153 func (t *testReadCloser) Close() error { 154 close(t.data) 155 return nil 156 } 157 158 func TestHelpers_LineLimitReader_TimeLimit(t *testing.T) { 159 // Create the test reader 160 in := &testReadCloser{data: make(chan []byte)} 161 162 // Set up the reader such that it won't hit the line/buffer limit and could 163 // only terminate if it hits the time limit 164 limit := NewLineLimitReader(in, 1000, 1000, 100*time.Millisecond) 165 166 expected := []byte("hello world") 167 168 resultCh := make(chan struct{}) 169 go func() { 170 outBytes, err := ioutil.ReadAll(limit) 171 if err != nil { 172 t.Fatalf("ReadAll failed: %v", err) 173 } 174 175 if reflect.DeepEqual(outBytes, expected) { 176 close(resultCh) 177 return 178 } 179 }() 180 181 // Send the data 182 in.data <- expected 183 in.Close() 184 185 select { 186 case <-resultCh: 187 case <-time.After(1 * time.Second): 188 t.Fatalf("did not exit by time limit") 189 } 190 } 191 192 const ( 193 job = `job "job1" { 194 type = "service" 195 datacenters = [ "dc1" ] 196 group "group1" { 197 count = 1 198 task "task1" { 199 driver = "exec" 200 resources = {} 201 } 202 restart{ 203 attempts = 10 204 mode = "delay" 205 } 206 } 207 }` 208 ) 209 210 // Test StructJob with local jobfile 211 func TestStructJobWithLocal(t *testing.T) { 212 fh, err := ioutil.TempFile("", "nomad") 213 if err != nil { 214 t.Fatalf("err: %s", err) 215 } 216 defer os.Remove(fh.Name()) 217 _, err = fh.WriteString(job) 218 if err != nil { 219 t.Fatalf("err: %s", err) 220 } 221 222 j := &JobGetter{} 223 sj, err := j.StructJob(fh.Name()) 224 if err != nil { 225 t.Fatalf("err: %s", err) 226 } 227 228 err = sj.Validate() 229 if err != nil { 230 t.Fatalf("err: %s", err) 231 } 232 } 233 234 // Test StructJob with jobfile from HTTP Server 235 func TestStructJobWithHTTPServer(t *testing.T) { 236 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 237 fmt.Fprintf(w, job) 238 }) 239 go http.ListenAndServe("127.0.0.1:12345", nil) 240 241 // Wait until HTTP Server starts certainly 242 time.Sleep(100 * time.Millisecond) 243 244 j := &JobGetter{} 245 sj, err := j.StructJob("http://127.0.0.1:12345/") 246 if err != nil { 247 t.Fatalf("err: %s", err) 248 } 249 250 err = sj.Validate() 251 if err != nil { 252 t.Fatalf("err: %s", err) 253 } 254 }