github.phpd.cn/cilium/cilium@v1.6.12/test/runtime/cli.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 "strconv" 20 "strings" 21 22 . "github.com/cilium/cilium/test/ginkgo-ext" 23 "github.com/cilium/cilium/test/helpers" 24 "github.com/cilium/cilium/test/helpers/constants" 25 26 . "github.com/onsi/gomega" 27 ) 28 29 var _ = Describe("RuntimeCLI", func() { 30 31 var vm *helpers.SSHMeta 32 33 BeforeAll(func() { 34 vm = helpers.InitRuntimeHelper(helpers.Runtime, logger) 35 ExpectCiliumReady(vm) 36 37 areEndpointsReady := vm.WaitEndpointsReady() 38 Expect(areEndpointsReady).Should(BeTrue()) 39 }) 40 41 JustAfterEach(func() { 42 vm.ValidateNoErrorsInLogs(CurrentGinkgoTestDescription().Duration) 43 }) 44 45 AfterFailed(func() { 46 vm.ReportFailed() 47 }) 48 49 AfterAll(func() { 50 vm.CloseSSHClient() 51 }) 52 53 Context("Identity CLI testing", func() { 54 55 var ( 56 fooID = "id.foo" 57 namesLabels = [][]string{{"foo", fooID}, {"bar", "id.bar"}, {"baz", "id.baz"}} 58 ) 59 60 BeforeAll(func() { 61 for _, set := range namesLabels { 62 res := vm.ContainerCreate(set[0], constants.NetperfImage, helpers.CiliumDockerNetwork, fmt.Sprintf("-l %s", set[1])) 63 res.ExpectSuccess("Unable to create container") 64 } 65 }) 66 67 AfterAll(func() { 68 for _, set := range namesLabels { 69 _ = vm.ContainerRm(set[0]) 70 } 71 }) 72 73 It("Test labelsSHA256", func() { 74 areEndpointsReady := vm.WaitEndpointsReady() 75 Expect(areEndpointsReady).Should(BeTrue(), "endpoints not ready") 76 77 epModel := vm.EndpointGet(fmt.Sprintf("-l container:%s", fooID)) 78 Expect(epModel).ShouldNot(BeNil(), "no endpoint model returned") 79 identity := epModel.Status.Identity.ID 80 81 out, err := vm.ExecCilium(fmt.Sprintf("identity get %d -o json", identity)).Filter("{[0].labelsSHA256}") 82 83 Expect(err).Should(BeNil(), "error getting SHA from identity") 84 fooSha := "7c5b1431262baa7f060728b6252abf6a42d9b39f38328d896b37755b1c578477" 85 Expect(out.String()).Should(Equal(fooSha)) 86 }) 87 88 It("test identity list", func() { 89 By("Testing 'cilium identity list' for an endpoint's identity") 90 91 areEndpointsReady := vm.WaitEndpointsReady() 92 Expect(areEndpointsReady).Should(BeTrue(), "endpoints not ready") 93 94 epModel := vm.EndpointGet(fmt.Sprintf("-l container:%s", fooID)) 95 Expect(epModel).ShouldNot(BeNil(), "no endpoint model returned") 96 identity := strconv.FormatInt(epModel.Status.Identity.ID, 10) 97 98 res := vm.ExecCilium(fmt.Sprintf("identity list container:%s", fooID)) 99 res.ExpectSuccess(fmt.Sprintf("Unable to get identity list output for label container:%s", fooID)) 100 101 resSingleOut := res.SingleOut() 102 103 containsIdentity := strings.Contains(resSingleOut, identity) 104 Expect(containsIdentity).To(BeTrue(), "identity %s from 'cilium endpoint get' for endpoint %s not in 'cilium identity list' output", identity, resSingleOut) 105 106 By("Testing 'cilium identity list' for reserved identities") 107 res = vm.Exec(`cilium identity list`) 108 resSingleOut = res.SingleOut() 109 110 reservedIdentities := []string{"health", "host", "world", "init"} 111 112 for _, id := range reservedIdentities { 113 By("checking that reserved identity '%s' is in 'cilium identity list' output", id) 114 containsReservedIdentity := strings.Contains(resSingleOut, id) 115 Expect(containsReservedIdentity).To(BeTrue(), "reserved identity '%s' not in 'cilium identity list' output", id) 116 } 117 }) 118 }) 119 120 Context("stdout stderr testing", func() { 121 122 It("root command help should print to stderr", func() { 123 res := vm.ExecCilium("help") 124 Expect(res.GetStdOut()).Should(BeEmpty()) 125 Expect(res.GetStdErr()).Should(ContainSubstring("Use \"cilium [command] --help\" for more information about a command.")) 126 }) 127 128 It("subcommand help should print to stderr", func() { 129 res := vm.ExecCilium("help bpf") 130 Expect(res.GetStdOut()).Should(BeEmpty()) 131 Expect(res.GetStdErr()).Should(ContainSubstring("Use \"cilium bpf [command] --help\" for more information about a command.")) 132 }) 133 134 It("failed subcommand should print help to stderr", func() { 135 res := vm.ExecCilium("endpoint confi 173") 136 Expect(res.GetStdOut()).Should(BeEmpty()) 137 Expect(res.GetStdErr()).Should(ContainSubstring("Use \"cilium endpoint [command] --help\" for more information about a command.")) 138 }) 139 140 }) 141 142 })