gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/ntpdate/ntpdate_test.go (about) 1 // Copyright 2017 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "bufio" 9 "strings" 10 "testing" 11 "time" 12 ) 13 14 var configFileTests = []struct { 15 config string 16 out []string 17 }{ 18 { 19 config: "", 20 out: []string{}, 21 }, 22 { 23 config: "server 127.0.0.1", 24 out: []string{"127.0.0.1"}, 25 }, 26 { 27 config: "server 127.0.0.1\n", 28 out: []string{"127.0.0.1"}, 29 }, 30 { 31 config: "servers 127.0.0.1", 32 out: []string{}, 33 }, 34 { 35 config: "server time.google.com", 36 out: []string{"time.google.com"}, 37 }, 38 { 39 config: "server 127.0.0.1\n" + 40 "server time.google.com", 41 out: []string{"127.0.0.1", "time.google.com"}, 42 }, 43 { 44 config: "servers 127.0.0.1\n" + 45 "server time.google.com", 46 out: []string{"time.google.com"}, 47 }, 48 } 49 50 func TestConfigParsing(t *testing.T) { 51 for _, tt := range configFileTests { 52 b := bufio.NewReader(strings.NewReader(tt.config)) 53 out := parseServers(b) 54 55 if len(out) != len(tt.out) { 56 t.Errorf("Different lengths! Expected:\n%v\ngot:\n%v", tt.out, out) 57 } 58 59 for i := range out { 60 if out[i] != tt.out[i] { 61 t.Errorf("Element at index %d differs. expected:\n%v\ngot:\n%v", i, tt.out, out) 62 } 63 } 64 } 65 } 66 67 var getTimeTests = []struct { 68 servers []string 69 time time.Time 70 err string 71 }{ 72 { 73 servers: []string{}, 74 err: "unable to get any time from servers", 75 }, 76 { 77 servers: []string{"nope.nothing.here"}, 78 err: "unable to get any time from servers", 79 }, 80 { 81 servers: []string{"nope.nothing.here", "nope.nothing.here2"}, 82 err: "unable to get any time from servers", 83 }, 84 } 85 86 func TestGetNoTime(t *testing.T) { 87 for _, tt := range getTimeTests { 88 _, err := getTime(tt.servers) 89 90 if err == nil { 91 t.Errorf("%v: got nil, want err", tt) 92 } 93 if !strings.HasPrefix(err.Error(), tt.err) { 94 t.Errorf("expected:\n%s\ngot:\n%s", tt.err, err.Error()) 95 } 96 } 97 }