github.com/looshlee/cilium@v1.6.12/test/runtime/examples.go (about)

     1  // Copyright 2018 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package RuntimeTest
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"path/filepath"
    21  	"strings"
    22  
    23  	. "github.com/cilium/cilium/test/ginkgo-ext"
    24  	"github.com/cilium/cilium/test/helpers"
    25  
    26  	. "github.com/onsi/gomega"
    27  )
    28  
    29  var _ = Describe("RuntimePolicyValidationTests", func() {
    30  	var vm *helpers.SSHMeta
    31  
    32  	BeforeAll(func() {
    33  		vm = helpers.InitRuntimeHelper(helpers.Runtime, logger)
    34  		ExpectCiliumReady(vm)
    35  	})
    36  
    37  	JustAfterEach(func() {
    38  		vm.ValidateNoErrorsInLogs(CurrentGinkgoTestDescription().Duration)
    39  	})
    40  
    41  	AfterFailed(func() {
    42  		vm.ReportFailed()
    43  	})
    44  
    45  	AfterAll(func() {
    46  		vm.CloseSSHClient()
    47  	})
    48  
    49  	It("Validates Example Policies", func() {
    50  		By("Validating Demos")
    51  
    52  		// Helper function which returns the path to all files in directory dir
    53  		// and all of dir's subdirectories with suffix extension. The file paths
    54  		// returned contain the path without the prefix dir. This allows for
    55  		// gathering of the list of files on the host and for the validation
    56  		// of the policy files to occur the VM, as the root directory of Cilium
    57  		// is different in each environment.
    58  		getFilesWithExtensionFromDir := func(dir, extension string) ([]string, error) {
    59  			fileNames := []string{}
    60  
    61  			walkFunc := func(path string, info os.FileInfo, err error) error {
    62  				if err != nil {
    63  					return err
    64  				}
    65  				if strings.HasSuffix(info.Name(), extension) {
    66  					relativePath := strings.TrimPrefix(path, dir)
    67  					fileNames = append(fileNames, relativePath)
    68  				}
    69  				return nil
    70  			}
    71  
    72  			err := filepath.Walk(dir, walkFunc)
    73  			if err != nil {
    74  				return nil, err
    75  			}
    76  
    77  			filesWithExtension := []string{}
    78  			for _, file := range fileNames {
    79  				if strings.HasSuffix(file, extension) {
    80  					filesWithExtension = append(filesWithExtension, file)
    81  				}
    82  			}
    83  			return filesWithExtension, nil
    84  		}
    85  
    86  		examplesDemoPath := "examples/demo"
    87  		examplesPoliciesPath := "examples/policies"
    88  		examplePathHost := filepath.Join("..", examplesDemoPath)
    89  		jsonFiles, err := getFilesWithExtensionFromDir(examplePathHost, "json")
    90  		Expect(err).Should(BeNil(), "Unable to get files at path %s: %s", examplePathHost, err)
    91  
    92  		examplePathVM := filepath.Join(helpers.BasePath, "..", examplesDemoPath)
    93  		for _, file := range jsonFiles {
    94  			jsonPolicyPath := filepath.Join(examplePathVM, file)
    95  			vm.ExecCilium(fmt.Sprintf("policy validate %s", jsonPolicyPath)).ExpectSuccess("Unable to validate policy %s", jsonPolicyPath)
    96  		}
    97  
    98  		By("Validating JSON Examples")
    99  
   100  		jsonExamplesPathHost := filepath.Join("..", examplesPoliciesPath)
   101  		jsonFiles, err = getFilesWithExtensionFromDir(jsonExamplesPathHost, "json")
   102  		Expect(err).Should(BeNil(), "Unable to get files at path %s: %s", jsonExamplesPathHost, err)
   103  
   104  		jsonExamplesPathVM := filepath.Join(helpers.BasePath, "..", examplesPoliciesPath)
   105  		for _, file := range jsonFiles {
   106  			jsonPolicyPath := filepath.Join(jsonExamplesPathVM, file)
   107  			vm.ExecCilium(fmt.Sprintf("policy validate %s", jsonPolicyPath)).ExpectSuccess("Unable to validate policy %s", jsonPolicyPath)
   108  		}
   109  
   110  		By("Validating YAML Examples")
   111  
   112  		yamlExamplesPathHost := filepath.Join("..", examplesPoliciesPath)
   113  		jsonFiles, err = getFilesWithExtensionFromDir(yamlExamplesPathHost, "yaml")
   114  		Expect(err).Should(BeNil(), "Unable to get files at path %s: %s", yamlExamplesPathHost, err)
   115  
   116  		yamlExamplesPathVM := filepath.Join(helpers.BasePath, "..", examplesPoliciesPath)
   117  		for _, file := range jsonFiles {
   118  			yamlPolicyPath := filepath.Join(yamlExamplesPathVM, file)
   119  			res := vm.Exec(fmt.Sprintf("yamllint -c %s %s", filepath.Join(helpers.BasePath, "yaml.config"), yamlPolicyPath))
   120  			res.ExpectSuccess("Unable to validate YAML %s", yamlPolicyPath)
   121  		}
   122  	})
   123  })