github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/controllers/core/cmd/probe_test.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 11 "github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1" 12 ) 13 14 func TestProbeFromSpecUnsupported(t *testing.T) { 15 // empty probe spec 16 probeSpec := &v1alpha1.Probe{} 17 p, err := proberFromSpec(&FakeProberManager{}, probeSpec) 18 assert.Nil(t, p) 19 assert.ErrorIs(t, err, ErrUnsupportedProbeType) 20 } 21 22 func TestProbeFromSpecTCP(t *testing.T) { 23 type tc struct { 24 host string 25 port int32 26 expectedErr string 27 } 28 cases := []tc{ 29 {"localhost", 8080, ""}, 30 {"localhost", 0, "port number out of range: 0"}, 31 {"localhost", -1, "port number out of range: -1"}, 32 {"localhost", 65536, "port number out of range: 65536"}, 33 {"localhost", 1234, ""}, 34 {"", 22, ""}, 35 } 36 for i, tc := range cases { 37 t.Run(fmt.Sprintf("[%d] %s:%d", i, tc.host, tc.port), func(t *testing.T) { 38 probeSpec := &v1alpha1.Probe{ 39 Handler: v1alpha1.Handler{ 40 TCPSocket: &v1alpha1.TCPSocketAction{ 41 Host: tc.host, 42 Port: tc.port, 43 }, 44 }, 45 } 46 manager := &FakeProberManager{} 47 p, err := proberFromSpec(manager, probeSpec) 48 if tc.expectedErr != "" { 49 require.EqualError(t, err, tc.expectedErr) 50 } else { 51 assert.Nil(t, err) 52 assert.NotNil(t, p) 53 if tc.host != "" { 54 assert.Equal(t, tc.host, manager.tcpHost) 55 } else { 56 assert.Equal(t, "localhost", manager.tcpHost) 57 } 58 assert.Equal(t, int(tc.port), manager.tcpPort) 59 } 60 }) 61 } 62 } 63 64 func TestProbeFromSpecHTTP(t *testing.T) { 65 type tc struct { 66 httpGet *v1alpha1.HTTPGetAction 67 expectedErr string 68 } 69 cases := []tc{ 70 { 71 &v1alpha1.HTTPGetAction{ 72 Host: "", 73 Port: 80, 74 }, 75 "", 76 }, 77 { 78 &v1alpha1.HTTPGetAction{ 79 Host: "localhost", 80 Port: -1, 81 }, 82 "port number out of range: -1", 83 }, 84 { 85 &v1alpha1.HTTPGetAction{ 86 Host: "localhost", 87 Port: 8080, 88 Scheme: v1alpha1.URISchemeHTTPS, 89 }, 90 "", 91 }, 92 { 93 &v1alpha1.HTTPGetAction{ 94 Host: "localhost", 95 Port: 8888, 96 HTTPHeaders: []v1alpha1.HTTPHeader{ 97 {Name: "X-Fake-Header", Value: "value-1"}, 98 {Name: "X-Fake-Header", Value: "value-2"}, 99 {Name: "Content-Type", Value: "application/json"}, 100 }, 101 }, 102 "", 103 }, 104 } 105 for i, tc := range cases { 106 t.Run(fmt.Sprintf("[%d] %v", i, tc.httpGet), func(t *testing.T) { 107 probeSpec := &v1alpha1.Probe{ 108 Handler: v1alpha1.Handler{ 109 HTTPGet: tc.httpGet, 110 }, 111 } 112 manager := &FakeProberManager{} 113 p, err := proberFromSpec(manager, probeSpec) 114 if tc.expectedErr != "" { 115 require.EqualError(t, err, tc.expectedErr) 116 } else { 117 assert.Nil(t, err) 118 assert.NotNil(t, p) 119 u := manager.httpURL 120 if tc.httpGet.Host != "" { 121 assert.Equal(t, tc.httpGet.Host, u.Hostname()) 122 } else { 123 assert.Equal(t, "localhost", u.Hostname()) 124 } 125 assert.Equal(t, fmt.Sprintf("%d", tc.httpGet.Port), u.Port()) 126 if tc.httpGet.Scheme != "" { 127 assert.Equal(t, strings.ToLower(string(tc.httpGet.Scheme)), u.Scheme) 128 } else { 129 assert.Equal(t, "http", u.Scheme) 130 } 131 assert.Equal(t, tc.httpGet.Path, u.Path) 132 for _, h := range tc.httpGet.HTTPHeaders { 133 assert.Contains(t, manager.httpHeaders[h.Name], h.Value) 134 } 135 } 136 }) 137 } 138 } 139 140 func TestProbeFromSpecExec(t *testing.T) { 141 cases := [][]string{ 142 {"echo"}, 143 {"echo", "arg1", "arg2"}, 144 } 145 for i, command := range cases { 146 t.Run(fmt.Sprintf("[%d] %s", i, command), func(t *testing.T) { 147 probeSpec := &v1alpha1.Probe{ 148 Handler: v1alpha1.Handler{ 149 Exec: &v1alpha1.ExecAction{ 150 Command: command, 151 }, 152 }, 153 } 154 manager := &FakeProberManager{} 155 p, err := proberFromSpec(manager, probeSpec) 156 assert.Nil(t, err) 157 assert.NotNil(t, p) 158 assert.Equal(t, command[0], manager.execName) 159 assert.Equal(t, command[1:], manager.execArgs) 160 }) 161 } 162 }