...

Source file src/edge-infra.dev/pkg/edge/edgeadmin/commands/operatorintervention/delete/command_test.go

Documentation: edge-infra.dev/pkg/edge/edgeadmin/commands/operatorintervention/delete

     1  //nolint:dupl
     2  package delete
     3  
     4  import (
     5  	"context"
     6  	"encoding/json"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"edge-infra.dev/pkg/edge/api/fake"
    14  	"edge-infra.dev/pkg/edge/api/graph/model"
    15  	"edge-infra.dev/pkg/edge/edgecli"
    16  	"edge-infra.dev/pkg/edge/edgecli/flagutil"
    17  	"edge-infra.dev/pkg/lib/cli/rags"
    18  )
    19  
    20  type DeleteOperatorInterventionCommandResponse struct {
    21  	DeleteOperatorInterventionCommand model.DeleteOperatorInterventionCommandResponse `json:"deleteOperatorInterventionCommand"`
    22  }
    23  
    24  // Mocks the DeleteOperatorInterventionCommandResponse. Returns an error
    25  // response when the command `bad-command` is sent, otherwise returns a
    26  // non-error response
    27  func DeleteOperatorInterventionCommand(body []byte) (interface{}, bool, error) {
    28  	if !strings.Contains(string(body), "deleteOperatorInterventionCommand(command: $command)") {
    29  		return nil, false, nil
    30  	}
    31  
    32  	// This callback should handle the response
    33  	var ok = true
    34  
    35  	var m struct {
    36  		Query     string
    37  		Variables struct {
    38  			Command struct {
    39  				Name string
    40  			}
    41  		}
    42  	}
    43  	err := json.Unmarshal(body, &m)
    44  	if err != nil {
    45  		return DeleteOperatorInterventionCommandResponse{}, ok, nil
    46  	}
    47  
    48  	badCommand := "bad-command"
    49  	var errors []*model.OperatorInterventionErrorResponse
    50  	if m.Variables.Command.Name == "bad-command" {
    51  		errors = []*model.OperatorInterventionErrorResponse{{
    52  			Type:    model.OperatorInterventionErrorTypeUnknownCommand,
    53  			Command: &badCommand,
    54  		}}
    55  	}
    56  	return DeleteOperatorInterventionCommandResponse{
    57  		DeleteOperatorInterventionCommand: model.DeleteOperatorInterventionCommandResponse{
    58  			Errors: errors,
    59  		},
    60  	}, ok, nil
    61  }
    62  
    63  func TestDeleteCommand(t *testing.T) {
    64  	t.Parallel()
    65  
    66  	edgeAPIMockSvr := fake.GetMockAPIServer(DeleteOperatorInterventionCommand)
    67  	edgeAPIMockURL := edgeAPIMockSvr.URL + "/api/v2"
    68  	t.Cleanup(func() {
    69  		edgeAPIMockSvr.Close()
    70  	})
    71  
    72  	tests := map[string]struct {
    73  		flagsFunc func(*rags.RagSet) error
    74  		expError  require.ErrorAssertionFunc
    75  	}{
    76  		"No Flags": {
    77  			flagsFunc: func(*rags.RagSet) error { return nil },
    78  			expError:  require.Error,
    79  		},
    80  		"Empty Flag": {
    81  			flagsFunc: func(c *rags.RagSet) error {
    82  				return flagutil.SetFlag(c, flagutil.OICommand, "")
    83  			},
    84  			expError: require.Error,
    85  		},
    86  		"Flag": {
    87  			flagsFunc: func(c *rags.RagSet) error {
    88  				return flagutil.SetFlag(c, flagutil.OICommand, "abcd")
    89  			},
    90  			expError: require.NoError,
    91  		},
    92  		"Bad Command": {
    93  			flagsFunc: func(c *rags.RagSet) error {
    94  				return flagutil.SetFlag(c, flagutil.OICommand, "bad-command")
    95  			},
    96  			expError: require.Error,
    97  		},
    98  	}
    99  
   100  	for name, tc := range tests {
   101  		tc := tc
   102  		t.Run(name, func(t *testing.T) {
   103  			t.Parallel()
   104  
   105  			// Set a dummy token in a fake banner context so that we bypass the
   106  			// ValidateConnectionFlags check which would otherwise do a login
   107  			// mutation to the api server, something which is not supported by
   108  			// the fake server
   109  			future := time.Now().Add(time.Hour * 24)
   110  			testConfig := edgecli.Config{
   111  				CurrentBannerContext: "fakeBanner",
   112  				BannerContexts: map[string]*edgecli.BannerContext{
   113  					"fakeBanner": {
   114  						TokenTime: future.Format(time.RFC3339),
   115  						Token:     "fakeToken",
   116  						Endpoint:  edgeAPIMockURL,
   117  					},
   118  				},
   119  			}
   120  
   121  			cmd := NewDeleteCommand(&testConfig)
   122  			cmd.Command() // Required to initialise cmd.Rags
   123  
   124  			require.NoError(t, tc.flagsFunc(cmd.Rags))
   125  
   126  			err := cmd.Command().Exec(context.Background(), []string{})
   127  			tc.expError(t, err)
   128  		})
   129  	}
   130  }
   131  

View as plain text