Skip to content

Commit 268c829

Browse files
Add test for exec
1 parent 7a75847 commit 268c829

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

internal/validator/exec_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
"testing"
24+
25+
canaryv1 "github.com/nvidia/container-canary/internal/apis/v1"
26+
"github.com/nvidia/container-canary/internal/container"
27+
"github.com/rs/zerolog"
28+
"github.com/stretchr/testify/assert"
29+
v1 "k8s.io/api/core/v1"
30+
)
31+
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+
61+
func TestExecSuccess(t *testing.T) {
62+
assert := assert.New(t)
63+
64+
c := &dummyContainer{}
65+
probe := &canaryv1.Probe{
66+
Exec: &v1.ExecAction{
67+
Command: []string{"success"},
68+
},
69+
}
70+
71+
logger, buf := logger()
72+
e := logger.Info()
73+
result, err := ExecCheck(c, probe, e)
74+
e.Send()
75+
assert.True(result)
76+
assert.NoError(err)
77+
assert.Equal("{\"level\":\"info\",\"exitCode\":0,\"stdout\":\"Success stdout\",\"stderr\":\"Success stderr\"}\n", buf.String())
78+
}
79+
80+
func TestExecFailure(t *testing.T) {
81+
assert := assert.New(t)
82+
83+
c := &dummyContainer{}
84+
probe := &canaryv1.Probe{
85+
Exec: &v1.ExecAction{
86+
Command: []string{"failure"},
87+
},
88+
}
89+
90+
logger, buf := logger()
91+
e := logger.Info()
92+
result, err := ExecCheck(c, probe, e)
93+
e.Send()
94+
assert.False(result)
95+
assert.NoError(err)
96+
assert.Equal("{\"level\":\"info\",\"exitCode\":1,\"stdout\":\"Failure stdout\",\"stderr\":\"Failure stderr\"}\n", buf.String())
97+
}
98+
99+
func TestExecError(t *testing.T) {
100+
assert := assert.New(t)
101+
102+
c := &dummyContainer{}
103+
probe := &canaryv1.Probe{
104+
Exec: &v1.ExecAction{
105+
Command: []string{"error"},
106+
},
107+
}
108+
109+
logger, buf := logger()
110+
e := logger.Info()
111+
result, err := ExecCheck(c, probe, e)
112+
e.Send()
113+
assert.False(result)
114+
assert.Error(err, "This command is an error")
115+
assert.Equal("{\"level\":\"info\"}\n", buf.String())
116+
}

0 commit comments

Comments
 (0)