github.com/jk-he/cni@v0.8.1/pkg/invoke/get_version_integration_test.go (about)

     1  // Copyright 2016 CNI authors
     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 invoke_test
    16  
    17  import (
    18  	"context"
    19  	"io/ioutil"
    20  	"os"
    21  	"path/filepath"
    22  	"runtime"
    23  
    24  	"github.com/containernetworking/cni/pkg/invoke"
    25  	"github.com/containernetworking/cni/pkg/version"
    26  	"github.com/containernetworking/cni/pkg/version/testhelpers"
    27  
    28  	. "github.com/onsi/ginkgo"
    29  	. "github.com/onsi/ginkgo/extensions/table"
    30  	. "github.com/onsi/gomega"
    31  )
    32  
    33  var _ = Describe("GetVersion, integration tests", func() {
    34  	var (
    35  		pluginDir  string
    36  		pluginPath string
    37  	)
    38  
    39  	BeforeEach(func() {
    40  		pluginDir, err := ioutil.TempDir("", "plugins")
    41  		Expect(err).NotTo(HaveOccurred())
    42  		pluginPath = filepath.Join(pluginDir, "test-plugin")
    43  		if runtime.GOOS == "windows" {
    44  			pluginPath += ".exe"
    45  		}
    46  	})
    47  
    48  	AfterEach(func() {
    49  		Expect(os.RemoveAll(pluginDir)).To(Succeed())
    50  	})
    51  
    52  	DescribeTable("correctly reporting plugin versions",
    53  		func(gitRef string, pluginSource string, expectedVersions version.PluginInfo) {
    54  			Expect(testhelpers.BuildAt([]byte(pluginSource), gitRef, pluginPath)).To(Succeed())
    55  			versionInfo, err := invoke.GetVersionInfo(context.TODO(), pluginPath, nil)
    56  			Expect(err).NotTo(HaveOccurred())
    57  
    58  			Expect(versionInfo.SupportedVersions()).To(ConsistOf(expectedVersions.SupportedVersions()))
    59  		},
    60  
    61  		Entry("historical: before VERSION was introduced",
    62  			git_ref_v010, plugin_source_no_custom_versions,
    63  			version.PluginSupports("0.1.0"),
    64  		),
    65  
    66  		Entry("historical: when VERSION was introduced but plugins couldn't customize it",
    67  			git_ref_v020_no_custom_versions, plugin_source_no_custom_versions,
    68  			version.PluginSupports("0.1.0", "0.2.0"),
    69  		),
    70  
    71  		Entry("historical: when plugins started reporting their own version list",
    72  			git_ref_v020_custom_versions, plugin_source_v020_custom_versions,
    73  			version.PluginSupports("0.2.0", "0.999.0"),
    74  		),
    75  
    76  		Entry("historical: before CHECK was introduced",
    77  			git_ref_v031, plugin_source_v020_custom_versions,
    78  			version.PluginSupports("0.2.0", "0.999.0"),
    79  		),
    80  
    81  		// this entry tracks the current behavior.  Before you change it, ensure
    82  		// that its previous behavior is captured in the most recent "historical" entry
    83  		Entry("current",
    84  			"HEAD", plugin_source_v040_check,
    85  			version.PluginSupports("0.2.0", "0.4.0", "0.999.0"),
    86  		),
    87  	)
    88  })
    89  
    90  // A 0.4.0 plugin that supports CHECK
    91  const plugin_source_v040_check = `package main
    92  
    93  import (
    94  	"github.com/containernetworking/cni/pkg/skel"
    95  	"github.com/containernetworking/cni/pkg/version"
    96  	"fmt"
    97  )
    98  
    99  func c(_ *skel.CmdArgs) error { fmt.Println("{}"); return nil }
   100  
   101  func main() { skel.PluginMain(c, c, c, version.PluginSupports("0.2.0", "0.4.0", "0.999.0"), "") }
   102  `
   103  
   104  const git_ref_v031 = "909fe7d"
   105  
   106  // a 0.2.0 plugin that can report its own versions
   107  const plugin_source_v020_custom_versions = `package main
   108  
   109  import (
   110  	"github.com/containernetworking/cni/pkg/skel"
   111  	"github.com/containernetworking/cni/pkg/version"
   112  	"fmt"
   113  )
   114  
   115  func c(_ *skel.CmdArgs) error { fmt.Println("{}"); return nil }
   116  
   117  func main() { skel.PluginMain(c, c, version.PluginSupports("0.2.0", "0.999.0")) }
   118  `
   119  const git_ref_v020_custom_versions = "bf31ed15"
   120  
   121  // a minimal 0.1.0 / 0.2.0 plugin that cannot report it's own version support
   122  const plugin_source_no_custom_versions = `package main
   123  
   124  import "github.com/containernetworking/cni/pkg/skel"
   125  import "fmt"
   126  
   127  func c(_ *skel.CmdArgs) error { fmt.Println("{}"); return nil }
   128  
   129  func main() { skel.PluginMain(c, c) }
   130  `
   131  
   132  const git_ref_v010 = "2c482f4"
   133  const git_ref_v020_no_custom_versions = "349d66d"