Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add crictl config e2e tests #1659

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions test/e2e/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
)

var _ = t.Describe("config", func() {
var configFile *os.File

BeforeEach(func() {
var err error
configFile, err = os.CreateTemp("", "crictl-*.yaml")
Expect(err).NotTo(HaveOccurred())
})

AfterEach(func() {
Expect(os.RemoveAll(configFile.Name())).NotTo(HaveOccurred())
})

listConfig := func() string {
res := t.Crictl("--config " + configFile.Name() + " config --list")
Expect(res).To(Exit(0))
return string(res.Out.Contents())
}

It("should succeed to list with empty config", func() {
cfg := listConfig()
Expect(cfg).To(ContainSubstring("runtime-endpoint"))
Expect(cfg).To(ContainSubstring("image-endpoint"))
Expect(cfg).To(MatchRegexp("timeout .* 0"))
Expect(cfg).To(MatchRegexp("debug .* false"))
Expect(cfg).To(MatchRegexp("pull-image-on-create .* false"))
Expect(cfg).To(MatchRegexp("disable-pull-on-run .* false"))
})

It("should succeed to set config values", func() {
t.CrictlExpectSuccess("--config "+configFile.Name()+" config --set runtime-endpoint=foo,timeout=10", "")

cfg := listConfig()
Expect(cfg).To(MatchRegexp("runtime-endpoint .* foo"))
Expect(cfg).To(MatchRegexp("timeout .* 10"))
})

It("should succeed to set config values and preserve comments", func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how it will treat duplicate entries.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will only change the first one, which is a bug. Going to fix that in #1661

Copy link
Member

@SergeyKanzhelev SergeyKanzhelev Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if preserving comments is a valuable feature. But I can see how it may be useful and some people relying on it, thank you for adding a test!

_, err := configFile.WriteString(`
runtime-endpoint: "foo"
image-endpoint: "bar" # an inline comment
timeout: 5

# comment below a newline
debug: true
`)
Expect(err).NotTo(HaveOccurred())

t.CrictlExpectSuccess("--config "+configFile.Name()+" config --set runtime-endpoint=bar,image-endpoint=baz,timeout=10,debug=false", "")

cfgContent, err := os.ReadFile(configFile.Name())
Expect(err).NotTo(HaveOccurred())

Expect(string(cfgContent)).To(Equal(
`runtime-endpoint: "bar"
image-endpoint: "baz" # an inline comment
timeout: 10
# comment below a newline
debug: false
pull-image-on-create: false
disable-pull-on-run: false
`))
})
})
Loading