Skip to content

Commit cdfafe6

Browse files
Add TCP test
1 parent 268c829 commit cdfafe6

File tree

3 files changed

+138
-33
lines changed

3 files changed

+138
-33
lines changed

internal/validator/exec_test.go

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,13 @@
1818
package validator
1919

2020
import (
21-
"bytes"
22-
"errors"
2321
"testing"
2422

2523
canaryv1 "github.com/nvidia/container-canary/internal/apis/v1"
26-
"github.com/nvidia/container-canary/internal/container"
27-
"github.com/rs/zerolog"
2824
"github.com/stretchr/testify/assert"
2925
v1 "k8s.io/api/core/v1"
3026
)
3127

32-
type dummyContainer struct{}
33-
34-
func (c *dummyContainer) Start() error { return nil }
35-
36-
func (c *dummyContainer) Remove() error { return nil }
37-
38-
func (c *dummyContainer) Status() (*container.ContainerInfo, error) { return nil, nil }
39-
40-
func (c *dummyContainer) Exec(command ...string) (exitCode int, stdout string, stderr string, err error) {
41-
switch command[0] {
42-
case "success":
43-
return 0, "Success stdout", "Success stderr", nil
44-
case "failure":
45-
return 1, "Failure stdout", "Failure stderr", nil
46-
case "error":
47-
return 0, "", "", errors.New("This command is an error")
48-
default:
49-
return 1, "", "", nil
50-
}
51-
}
52-
53-
func (c *dummyContainer) Logs() (string, error) { return "", nil }
54-
55-
func logger() (zerolog.Logger, *bytes.Buffer) {
56-
buf := new(bytes.Buffer)
57-
logger := zerolog.New(buf)
58-
return logger, buf
59-
}
60-
6128
func TestExecSuccess(t *testing.T) {
6229
assert := assert.New(t)
6330

@@ -72,6 +39,7 @@ func TestExecSuccess(t *testing.T) {
7239
e := logger.Info()
7340
result, err := ExecCheck(c, probe, e)
7441
e.Send()
42+
7543
assert.True(result)
7644
assert.NoError(err)
7745
assert.Equal("{\"level\":\"info\",\"exitCode\":0,\"stdout\":\"Success stdout\",\"stderr\":\"Success stderr\"}\n", buf.String())

internal/validator/tcp_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) <2024> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package validator
19+
20+
import (
21+
"net"
22+
"sync"
23+
"testing"
24+
25+
canaryv1 "github.com/nvidia/container-canary/internal/apis/v1"
26+
"github.com/stretchr/testify/require"
27+
)
28+
29+
func TestTCPSocketSuccess(t *testing.T) {
30+
require := require.New(t)
31+
32+
c := &dummyContainer{}
33+
probe := &canaryv1.Probe{
34+
TCPSocket: &canaryv1.TCPSocketAction{
35+
Port: 54321,
36+
},
37+
}
38+
39+
var wg sync.WaitGroup
40+
wg.Add(1)
41+
listener, err := net.Listen("tcp", "localhost:54321")
42+
require.NoError(err)
43+
success := false
44+
go func() {
45+
conn, err := listener.Accept()
46+
require.NoError(err)
47+
conn.Close()
48+
success = true
49+
wg.Done()
50+
}()
51+
52+
logger, buf := logger()
53+
e := logger.Info()
54+
result, err := TCPSocketCheck(c, probe, e)
55+
e.Send()
56+
57+
require.True(result)
58+
require.NoError(err)
59+
require.Equal("{\"level\":\"info\"}\n", buf.String())
60+
wg.Wait()
61+
require.True(success)
62+
}
63+
64+
func TestTCPSocketFailure(t *testing.T) {
65+
require := require.New(t)
66+
67+
c := &dummyContainer{}
68+
probe := &canaryv1.Probe{
69+
TCPSocket: &canaryv1.TCPSocketAction{
70+
Port: 54321,
71+
},
72+
}
73+
74+
logger, buf := logger()
75+
e := logger.Info()
76+
result, err := TCPSocketCheck(c, probe, e)
77+
e.Send()
78+
79+
require.False(result)
80+
require.NoError(err)
81+
require.Equal("{\"level\":\"info\"}\n", buf.String())
82+
}

internal/validator/utils_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) <2024> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package validator
19+
20+
import (
21+
"bytes"
22+
"errors"
23+
24+
"github.com/nvidia/container-canary/internal/container"
25+
"github.com/rs/zerolog"
26+
)
27+
28+
type dummyContainer struct{}
29+
30+
func (c *dummyContainer) Start() error { return nil }
31+
32+
func (c *dummyContainer) Remove() error { return nil }
33+
34+
func (c *dummyContainer) Status() (*container.ContainerInfo, error) { return nil, nil }
35+
36+
func (c *dummyContainer) Exec(command ...string) (exitCode int, stdout string, stderr string, err error) {
37+
switch command[0] {
38+
case "success":
39+
return 0, "Success stdout", "Success stderr", nil
40+
case "failure":
41+
return 1, "Failure stdout", "Failure stderr", nil
42+
case "error":
43+
return 0, "", "", errors.New("This command is an error")
44+
default:
45+
return 1, "", "", nil
46+
}
47+
}
48+
49+
func (c *dummyContainer) Logs() (string, error) { return "", nil }
50+
51+
func logger() (zerolog.Logger, *bytes.Buffer) {
52+
buf := new(bytes.Buffer)
53+
logger := zerolog.New(buf)
54+
return logger, buf
55+
}

0 commit comments

Comments
 (0)