Skip to content

Commit

Permalink
ephemeral-disk-utils: add few harmless tests
Browse files Browse the repository at this point in the history
Introduce a few tests to document RemoveFile and FileExists.

Signed-off-by: Dan Kenigsberg <[email protected]>
  • Loading branch information
dankenigsberg committed Jan 30, 2021
1 parent fd22afd commit 3fe3349
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/ephemeral-disk-utils/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
Expand All @@ -10,3 +10,17 @@ go_library(
"//staging/src/kubevirt.io/client-go/log:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = [
"utils_suite_test.go",
"utils_test.go",
],
embed = [":go_default_library"],
deps = [
"//staging/src/kubevirt.io/client-go/log:go_default_library",
"//vendor/github.com/onsi/ginkgo:go_default_library",
"//vendor/github.com/onsi/gomega:go_default_library",
],
)
35 changes: 35 additions & 0 deletions pkg/ephemeral-disk-utils/utils_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This file is part of the KubeVirt project
*
* 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.
*
* Copyright 2021 Red Hat, Inc.
*
*/

package ephemeraldiskutils

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"kubevirt.io/client-go/log"
)

func TestEphemeralDiskUtils(t *testing.T) {
log.Log.SetIOWriter(GinkgoWriter)
RegisterFailHandler(Fail)
RunSpecs(t, "EphemeralDiskUtils Suite")
}
51 changes: 51 additions & 0 deletions pkg/ephemeral-disk-utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This file is part of the KubeVirt project
*
* 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.
*
* Copyright 2021 Red Hat, Inc.
*
*/

package ephemeraldiskutils

import (
"io/ioutil"

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

var _ = Describe("FileExists", func() {
It("recognizes am existing file", func() {
Expect(FileExists("/etc/passwd")).To(BeTrue())
})
It("recognizes non-existing file", func() {
Expect(FileExists("no one would ever have this file")).To(BeFalse())
})
})
var _ = Describe("RemoveFile", func() {
It("silently ignores non-existing file", func() {
// Ingnoring missing files is typically a sloppy behavior. This test
// documents it, not aproves of its usage.
Expect(RemoveFile("no one would ever have this file")).To(BeNil())
})
It("removes a file", func() {
tmpfile, err := ioutil.TempFile("", "file_to_remove")
Expect(err).To(BeNil())
defer tmpfile.Close()
Expect(FileExists(tmpfile.Name())).To(BeTrue())
Expect(RemoveFile(tmpfile.Name())).To(BeNil())
Expect(FileExists(tmpfile.Name())).To(BeFalse())
})
})

0 comments on commit 3fe3349

Please sign in to comment.