k8s.io/kubernetes@v1.29.3/test/e2e/framework/testfiles/testfiles_test.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package testfiles
    18  
    19  import (
    20  	"embed"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  var (
    27  	fooContents = `Hello World
    28  `
    29  	fooPath = "testdata/a/foo.txt"
    30  
    31  	notExistsPath = "testdata/b"
    32  
    33  	expectedDescription = `The following files are embedded into the test executable:
    34  	testdata/a/foo.txt`
    35  )
    36  
    37  //go:embed testdata/a
    38  var testFS embed.FS
    39  
    40  func getTestEmbeddedSource() *EmbeddedFileSource {
    41  	return &EmbeddedFileSource{
    42  		EmbeddedFS: testFS,
    43  	}
    44  }
    45  
    46  func TestEmbeddedFileSource(t *testing.T) {
    47  	s := getTestEmbeddedSource()
    48  
    49  	// read a file which exists and compare the contents
    50  	b, err := s.ReadTestFile(fooPath)
    51  
    52  	assert.NoError(t, err)
    53  	assert.Equal(t, fooContents, string(b))
    54  
    55  	// read a non-existent file and ensure that the returned value is empty and error is nil
    56  	// Note: this is done so that the next file source can be tried by the caller
    57  	b, err = s.ReadTestFile(notExistsPath)
    58  	assert.NoError(t, err)
    59  	assert.Empty(t, b)
    60  
    61  	// describing the test filesystem should list down all files
    62  	assert.Equal(t, expectedDescription, s.DescribeFiles())
    63  }