github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/googlepubsub/googlepubsub_test.go (about)

     1  package googlepubsub_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/fastly/go-fastly/v9/fastly"
     8  
     9  	"github.com/fastly/cli/pkg/argparser"
    10  	"github.com/fastly/cli/pkg/commands/logging/googlepubsub"
    11  	"github.com/fastly/cli/pkg/config"
    12  	"github.com/fastly/cli/pkg/errors"
    13  	"github.com/fastly/cli/pkg/global"
    14  	"github.com/fastly/cli/pkg/manifest"
    15  	"github.com/fastly/cli/pkg/mock"
    16  	"github.com/fastly/cli/pkg/testutil"
    17  )
    18  
    19  func TestCreateGooglePubSubInput(t *testing.T) {
    20  	for _, testcase := range []struct {
    21  		name      string
    22  		cmd       *googlepubsub.CreateCommand
    23  		want      *fastly.CreatePubsubInput
    24  		wantError string
    25  	}{
    26  		{
    27  			name: "required values set flag serviceID",
    28  			cmd:  createCommandRequired(),
    29  			want: &fastly.CreatePubsubInput{
    30  				ServiceID:      "123",
    31  				ServiceVersion: 4,
    32  				Name:           fastly.ToPointer("log"),
    33  				User:           fastly.ToPointer("user@example.com"),
    34  				SecretKey:      fastly.ToPointer("secret"),
    35  				ProjectID:      fastly.ToPointer("project"),
    36  				Topic:          fastly.ToPointer("topic"),
    37  			},
    38  		},
    39  		{
    40  			name: "all values set flag serviceID",
    41  			cmd:  createCommandAll(),
    42  			want: &fastly.CreatePubsubInput{
    43  				ServiceID:         "123",
    44  				ServiceVersion:    4,
    45  				Name:              fastly.ToPointer("logs"),
    46  				Topic:             fastly.ToPointer("topic"),
    47  				User:              fastly.ToPointer("user@example.com"),
    48  				SecretKey:         fastly.ToPointer("secret"),
    49  				ProjectID:         fastly.ToPointer("project"),
    50  				FormatVersion:     fastly.ToPointer(2),
    51  				Format:            fastly.ToPointer(`%h %l %u %t "%r" %>s %b`),
    52  				ResponseCondition: fastly.ToPointer("Prevent default logging"),
    53  				Placement:         fastly.ToPointer("none"),
    54  			},
    55  		},
    56  		{
    57  			name:      "error missing serviceID",
    58  			cmd:       createCommandMissingServiceID(),
    59  			want:      nil,
    60  			wantError: errors.ErrNoServiceID.Error(),
    61  		},
    62  	} {
    63  		t.Run(testcase.name, func(t *testing.T) {
    64  			var bs []byte
    65  			out := bytes.NewBuffer(bs)
    66  			verboseMode := true
    67  
    68  			serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
    69  				AutoCloneFlag:      testcase.cmd.AutoClone,
    70  				APIClient:          testcase.cmd.Globals.APIClient,
    71  				Manifest:           testcase.cmd.Manifest,
    72  				Out:                out,
    73  				ServiceVersionFlag: testcase.cmd.ServiceVersion,
    74  				VerboseMode:        verboseMode,
    75  			})
    76  
    77  			switch {
    78  			case err != nil && testcase.wantError == "":
    79  				t.Fatalf("unexpected error getting service details: %v", err)
    80  				return
    81  			case err != nil && testcase.wantError != "":
    82  				testutil.AssertErrorContains(t, err, testcase.wantError)
    83  				return
    84  			case err == nil && testcase.wantError != "":
    85  				t.Fatalf("expected error, have nil (service details: %s, %d)", serviceID, serviceVersion.Number)
    86  			case err == nil && testcase.wantError == "":
    87  				have, err := testcase.cmd.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
    88  				testutil.AssertErrorContains(t, err, testcase.wantError)
    89  				testutil.AssertEqual(t, testcase.want, have)
    90  			}
    91  		})
    92  	}
    93  }
    94  
    95  func TestUpdateGooglePubSubInput(t *testing.T) {
    96  	scenarios := []struct {
    97  		name      string
    98  		cmd       *googlepubsub.UpdateCommand
    99  		api       mock.API
   100  		want      *fastly.UpdatePubsubInput
   101  		wantError string
   102  	}{
   103  		{
   104  			name: "all values set flag serviceID",
   105  			cmd:  updateCommandAll(),
   106  			api: mock.API{
   107  				ListVersionsFn: testutil.ListVersions,
   108  				CloneVersionFn: testutil.CloneVersionResult(4),
   109  				GetPubsubFn:    getGooglePubSubOK,
   110  			},
   111  			want: &fastly.UpdatePubsubInput{
   112  				ServiceID:         "123",
   113  				ServiceVersion:    4,
   114  				Name:              "log",
   115  				NewName:           fastly.ToPointer("new1"),
   116  				User:              fastly.ToPointer("new2"),
   117  				SecretKey:         fastly.ToPointer("new3"),
   118  				ProjectID:         fastly.ToPointer("new4"),
   119  				Topic:             fastly.ToPointer("new5"),
   120  				Placement:         fastly.ToPointer("new6"),
   121  				Format:            fastly.ToPointer("new7"),
   122  				FormatVersion:     fastly.ToPointer(3),
   123  				ResponseCondition: fastly.ToPointer("new8"),
   124  			},
   125  		},
   126  		{
   127  			name: "no updates",
   128  			cmd:  updateCommandNoUpdates(),
   129  			api: mock.API{
   130  				ListVersionsFn: testutil.ListVersions,
   131  				CloneVersionFn: testutil.CloneVersionResult(4),
   132  				GetPubsubFn:    getGooglePubSubOK,
   133  			},
   134  			want: &fastly.UpdatePubsubInput{
   135  				ServiceID:      "123",
   136  				ServiceVersion: 4,
   137  				Name:           "log",
   138  			},
   139  		},
   140  		{
   141  			name:      "error missing serviceID",
   142  			cmd:       updateCommandMissingServiceID(),
   143  			want:      nil,
   144  			wantError: errors.ErrNoServiceID.Error(),
   145  		},
   146  	}
   147  	for testcaseIdx := range scenarios {
   148  		testcase := &scenarios[testcaseIdx]
   149  		t.Run(testcase.name, func(t *testing.T) {
   150  			testcase.cmd.Globals.APIClient = testcase.api
   151  
   152  			var bs []byte
   153  			out := bytes.NewBuffer(bs)
   154  			verboseMode := true
   155  
   156  			serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   157  				AutoCloneFlag:      testcase.cmd.AutoClone,
   158  				APIClient:          testcase.api,
   159  				Manifest:           testcase.cmd.Manifest,
   160  				Out:                out,
   161  				ServiceVersionFlag: testcase.cmd.ServiceVersion,
   162  				VerboseMode:        verboseMode,
   163  			})
   164  
   165  			switch {
   166  			case err != nil && testcase.wantError == "":
   167  				t.Fatalf("unexpected error getting service details: %v", err)
   168  				return
   169  			case err != nil && testcase.wantError != "":
   170  				testutil.AssertErrorContains(t, err, testcase.wantError)
   171  				return
   172  			case err == nil && testcase.wantError != "":
   173  				t.Fatalf("expected error, have nil (service details: %s, %d)", serviceID, serviceVersion.Number)
   174  			case err == nil && testcase.wantError == "":
   175  				have, err := testcase.cmd.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   176  				testutil.AssertErrorContains(t, err, testcase.wantError)
   177  				testutil.AssertEqual(t, testcase.want, have)
   178  			}
   179  		})
   180  	}
   181  }
   182  
   183  func createCommandRequired() *googlepubsub.CreateCommand {
   184  	var b bytes.Buffer
   185  
   186  	g := global.Data{
   187  		Config: config.File{},
   188  		Env:    config.Environment{},
   189  		Output: &b,
   190  	}
   191  	g.APIClient, _ = mock.APIClient(mock.API{
   192  		ListVersionsFn: testutil.ListVersions,
   193  		CloneVersionFn: testutil.CloneVersionResult(4),
   194  	})("token", "endpoint", false)
   195  
   196  	return &googlepubsub.CreateCommand{
   197  		Base: argparser.Base{
   198  			Globals: &g,
   199  		},
   200  		Manifest: manifest.Data{
   201  			Flag: manifest.Flag{
   202  				ServiceID: "123",
   203  			},
   204  		},
   205  		ServiceVersion: argparser.OptionalServiceVersion{
   206  			OptionalString: argparser.OptionalString{Value: "1"},
   207  		},
   208  		AutoClone: argparser.OptionalAutoClone{
   209  			OptionalBool: argparser.OptionalBool{
   210  				Optional: argparser.Optional{
   211  					WasSet: true,
   212  				},
   213  				Value: true,
   214  			},
   215  		},
   216  		EndpointName: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "log"},
   217  		User:         argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "user@example.com"},
   218  		SecretKey:    argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "secret"},
   219  		ProjectID:    argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "project"},
   220  		Topic:        argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "topic"},
   221  	}
   222  }
   223  
   224  func createCommandAll() *googlepubsub.CreateCommand {
   225  	var b bytes.Buffer
   226  
   227  	g := global.Data{
   228  		Config: config.File{},
   229  		Env:    config.Environment{},
   230  		Output: &b,
   231  	}
   232  	g.APIClient, _ = mock.APIClient(mock.API{
   233  		ListVersionsFn: testutil.ListVersions,
   234  		CloneVersionFn: testutil.CloneVersionResult(4),
   235  	})("token", "endpoint", false)
   236  
   237  	return &googlepubsub.CreateCommand{
   238  		Base: argparser.Base{
   239  			Globals: &g,
   240  		},
   241  		Manifest: manifest.Data{
   242  			Flag: manifest.Flag{
   243  				ServiceID: "123",
   244  			},
   245  		},
   246  		ServiceVersion: argparser.OptionalServiceVersion{
   247  			OptionalString: argparser.OptionalString{Value: "1"},
   248  		},
   249  		AutoClone: argparser.OptionalAutoClone{
   250  			OptionalBool: argparser.OptionalBool{
   251  				Optional: argparser.Optional{
   252  					WasSet: true,
   253  				},
   254  				Value: true,
   255  			},
   256  		},
   257  		EndpointName:      argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "logs"},
   258  		User:              argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "user@example.com"},
   259  		ProjectID:         argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "project"},
   260  		Topic:             argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "topic"},
   261  		SecretKey:         argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "secret"},
   262  		Format:            argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: `%h %l %u %t "%r" %>s %b`},
   263  		FormatVersion:     argparser.OptionalInt{Optional: argparser.Optional{WasSet: true}, Value: 2},
   264  		ResponseCondition: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "Prevent default logging"},
   265  		Placement:         argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "none"},
   266  	}
   267  }
   268  
   269  func createCommandMissingServiceID() *googlepubsub.CreateCommand {
   270  	res := createCommandAll()
   271  	res.Manifest = manifest.Data{}
   272  	return res
   273  }
   274  
   275  func updateCommandNoUpdates() *googlepubsub.UpdateCommand {
   276  	var b bytes.Buffer
   277  
   278  	g := global.Data{
   279  		Config: config.File{},
   280  		Env:    config.Environment{},
   281  		Output: &b,
   282  	}
   283  
   284  	return &googlepubsub.UpdateCommand{
   285  		Base: argparser.Base{
   286  			Globals: &g,
   287  		},
   288  		Manifest: manifest.Data{
   289  			Flag: manifest.Flag{
   290  				ServiceID: "123",
   291  			},
   292  		},
   293  		EndpointName: "log",
   294  		ServiceVersion: argparser.OptionalServiceVersion{
   295  			OptionalString: argparser.OptionalString{Value: "1"},
   296  		},
   297  		AutoClone: argparser.OptionalAutoClone{
   298  			OptionalBool: argparser.OptionalBool{
   299  				Optional: argparser.Optional{
   300  					WasSet: true,
   301  				},
   302  				Value: true,
   303  			},
   304  		},
   305  	}
   306  }
   307  
   308  func updateCommandAll() *googlepubsub.UpdateCommand {
   309  	var b bytes.Buffer
   310  
   311  	g := global.Data{
   312  		Config: config.File{},
   313  		Env:    config.Environment{},
   314  		Output: &b,
   315  	}
   316  
   317  	return &googlepubsub.UpdateCommand{
   318  		Base: argparser.Base{
   319  			Globals: &g,
   320  		},
   321  		Manifest: manifest.Data{
   322  			Flag: manifest.Flag{
   323  				ServiceID: "123",
   324  			},
   325  		},
   326  		EndpointName: "log",
   327  		ServiceVersion: argparser.OptionalServiceVersion{
   328  			OptionalString: argparser.OptionalString{Value: "1"},
   329  		},
   330  		AutoClone: argparser.OptionalAutoClone{
   331  			OptionalBool: argparser.OptionalBool{
   332  				Optional: argparser.Optional{
   333  					WasSet: true,
   334  				},
   335  				Value: true,
   336  			},
   337  		},
   338  		NewName:           argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new1"},
   339  		User:              argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new2"},
   340  		SecretKey:         argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new3"},
   341  		ProjectID:         argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new4"},
   342  		Topic:             argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new5"},
   343  		Placement:         argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new6"},
   344  		Format:            argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new7"},
   345  		FormatVersion:     argparser.OptionalInt{Optional: argparser.Optional{WasSet: true}, Value: 3},
   346  		ResponseCondition: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "new8"},
   347  	}
   348  }
   349  
   350  func updateCommandMissingServiceID() *googlepubsub.UpdateCommand {
   351  	res := updateCommandAll()
   352  	res.Manifest = manifest.Data{}
   353  	return res
   354  }