"Fossies" - the Fresh Open Source Software Archive

Member "istio-1.6.5/mixer/tools/mixgen/cmd/adapter_test.go" (8 Jul 2020, 8832 Bytes) of package /linux/misc/istio-1.6.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2018 Istio 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 // nolint:lll
   16 //go:generate $REPO_ROOT/bin/protoc.sh testdata/simple/foo.proto -otestdata/simple/foo.descriptor --include_imports -I.
   17 //go:generate $REPO_ROOT/bin/protoc.sh testdata/simple/foo.proto -otestdata/simple/foo_without_imports.descriptor -I.
   18 package cmd
   19 
   20 import (
   21     "fmt"
   22     "strings"
   23     "testing"
   24 )
   25 
   26 type adapterCmdTestdata struct {
   27     configFile   string
   28     description  string
   29     name         string
   30     sessionBased bool
   31     templates    []string
   32     wantCfg      string
   33     wantErr      string
   34 }
   35 
   36 func TestAdapterCmd(t *testing.T) {
   37     for i, td := range []adapterCmdTestdata{
   38         {
   39             description:  "mydescription",
   40             configFile:   "testdata/simple/foo.descriptor",
   41             name:         "myresName",
   42             sessionBased: false,
   43             templates:    []string{"foo.bar", "foo1.bar1"},
   44             // nolint:lll
   45             wantCfg: `# this config is created through command
   46 # mixgen adapter -n myresName -c testdata/simple/foo.descriptor --description mydescription -s=false -t=foo.bar -t=foo1.bar1
   47 apiVersion: "config.istio.io/v1alpha2"
   48 kind: adapter
   49 metadata:
   50   name: myresName
   51   namespace: istio-system
   52 spec:
   53   description: mydescription
   54   session_based: false
   55   templates:
   56   - foo.bar
   57   - foo1.bar1
   58   config: CvMBCh5nb29nbGUvcHJvdG9idWYvZHVyYXRpb24ucHJvdG8SD2dvb2dsZS5wcm90b2J1ZiI6CghEdXJhdGlvbhIYCgdzZWNvbmRzGAEgASgDUgdzZWNvbmRzEhQKBW5hbm9zGAIgASgFUgVuYW5vc0J8ChNjb20uZ29vZ2xlLnByb3RvYnVmQg1EdXJhdGlvblByb3RvUAFaKmdpdGh1Yi5jb20vZ29sYW5nL3Byb3RvYnVmL3B0eXBlcy9kdXJhdGlvbvgBAaICA0dQQqoCHkdvb2dsZS5Qcm90b2J1Zi5XZWxsS25vd25UeXBlc2IGcHJvdG8zCvcBCh9nb29nbGUvcHJvdG9idWYvdGltZXN0YW1wLnByb3RvEg9nb29nbGUucHJvdG9idWYiOwoJVGltZXN0YW1wEhgKB3NlY29uZHMYASABKANSB3NlY29uZHMSFAoFbmFub3MYAiABKAVSBW5hbm9zQn4KE2NvbS5nb29nbGUucHJvdG9idWZCDlRpbWVzdGFtcFByb3RvUAFaK2dpdGh1Yi5jb20vZ29sYW5nL3Byb3RvYnVmL3B0eXBlcy90aW1lc3RhbXD4AQGiAgNHUEKqAh5Hb29nbGUuUHJvdG9idWYuV2VsbEtub3duVHlwZXNiBnByb3RvMwrNCQoZcG9saWN5L3YxYmV0YTEvdHlwZS5wcm90bxIUaXN0aW8ucG9saWN5LnYxYmV0YTEaHmdvb2dsZS9wcm90b2J1Zi9kdXJhdGlvbi5wcm90bxofZ29vZ2xlL3Byb3RvYnVmL3RpbWVzdGFtcC5wcm90byKkBQoFVmFsdWUSIwoMc3RyaW5nX3ZhbHVlGAEgASgJSABSC3N0cmluZ1ZhbHVlEiEKC2ludDY0X3ZhbHVlGAIgASgDSABSCmludDY0VmFsdWUSIwoMZG91YmxlX3ZhbHVlGAMgASgBSABSC2RvdWJsZVZhbHVlEh8KCmJvb2xfdmFsdWUYBCABKAhIAFIJYm9vbFZh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HJ5UgV2YWx1ZRo4CgpWYWx1ZUVudHJ5EhAKA2tleRgBIAEoCVIDa2V5EhQKBXZhbHVlGAIgASgJUgV2YWx1ZToCOAEiJAoMRW1haWxBZGRyZXNzEhQKBXZhbHVlGAEgASgJUgV2YWx1ZSIbCgNVcmkSFAoFdmFsdWUYASABKAlSBXZhbHVlQh1aG2lzdGlvLmlvL2FwaS9wb2xpY3kvdjFiZXRhMWIGcHJvdG8zCo0BChl0ZXN0ZGF0YS9zaW1wbGUvZm9vLnByb3RvEhlpc3Rpby5taXhlci5hZGFwdGVyLnF1b3RhGhlwb2xpY3kvdjFiZXRhMS90eXBlLnByb3RvIjIKAWESLQoDdmFsGAEgASgLMhsuaXN0aW8ucG9saWN5LnYxYmV0YTEuVmFsdWVSA3ZhbGIGcHJvdG8z
   59 ---
   60 `,
   61         },
   62         {
   63             description:  "mydescription",
   64             configFile:   "testdata/simple/foo.descriptor",
   65             name:         "myresName",
   66             sessionBased: true,
   67             // nolint:lll
   68             wantCfg: `# this config is created through command
   69 # mixgen adapter -n myresName -c testdata/simple/foo.descriptor --description mydescription -s=true
   70 apiVersion: "config.istio.io/v1alpha2"
   71 kind: adapter
   72 metadata:
   73   name: myresName
   74   namespace: istio-system
   75 spec:
   76   description: mydescription
   77   session_based: true
   78   templates:
   79   config: CvMBCh5nb29nbGUvcHJvdG9idWYvZHVyYXRpb24ucHJvdG8SD2dvb2dsZS5wcm90b2J1ZiI6CghEdXJhdGlvbhIYCgdzZWNvbmRzGAEgASgDUgdzZWNvbmRzEhQKBW5hbm9zGAIgASgFUgVuYW5vc0J8ChNjb20uZ29vZ2xlLnByb3RvYnVmQg1EdXJhdGlvblByb3RvUAFaKmdpdGh1Yi5jb20vZ29sYW5nL3Byb3RvYnVmL3B0eXBlcy9kdXJhdGlvbvgBAaICA0dQQqoCHkdvb2dsZS5Qcm90b2J1Zi5XZWxsS25vd25UeXBlc2IGcHJvdG8zCvcBCh9nb29nbGUvcHJvdG9idWYvdGltZXN0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GcHJvdG8z
   80 ---
   81 `,
   82         },
   83         {
   84             configFile: "testdata/simple/foo_without_imports.descriptor",
   85             wantErr: "config in invalid: the file descriptor set was created without including imports. Please run " +
   86                 "protoc with `--include_imports` flag",
   87         },
   88     } {
   89         t.Run(fmt.Sprintf("%d", i), func(tt *testing.T) {
   90             args := getArgs(td, td.configFile)
   91             gotCfg := ""
   92             root := GetRootCmd(args,
   93                 func(format string, a ...interface{}) {
   94                     gotCfg = fmt.Sprintf(format, a...)
   95                 },
   96                 func(format string, a ...interface{}) {
   97                     gotError := fmt.Sprintf(format, a...)
   98                     if td.wantErr == "" {
   99                         tt.Fatalf("want error 'nil'; got '%s'", gotError)
  100                         return
  101                     }
  102                     if !strings.Contains(gotError, td.wantErr) {
  103                         tt.Fatalf("want error '%s'; got '%s'", td.wantErr, gotError)
  104                     }
  105                 })
  106 
  107             _ = root.Execute()
  108 
  109             if td.wantErr == "" && gotCfg != td.wantCfg {
  110                 tt.Errorf("want :\n%v\ngot :\n%v", td.wantCfg, gotCfg)
  111             }
  112         })
  113     }
  114 }
  115 
  116 func TestAdapterCmd_NoInputFile(t *testing.T) {
  117     var gotError string
  118     cmd := GetRootCmd([]string{"adapter"},
  119         func(format string, a ...interface{}) {},
  120         func(format string, a ...interface{}) {
  121             gotError = fmt.Sprintf(format, a...)
  122             t.Fatalf("want no error; got '%s'", gotError)
  123         })
  124     _ = cmd.Execute()
  125 }
  126 
  127 func getArgs(td adapterCmdTestdata, fName string) []string {
  128     args := []string{
  129         "adapter",
  130         "-n", td.name,
  131         "-c", fName,
  132         "--description", td.description,
  133         "-s=" + fmt.Sprintf("%v", td.sessionBased),
  134     }
  135     for _, tmpl := range td.templates {
  136         args = append(args, fmt.Sprintf("-t=%s", tmpl))
  137     }
  138     return args
  139 }