github.com/kyma-project/kyma-environment-broker@v0.0.1/common/director/client_test.go (about)

     1  package director
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
     8  	mocks "github.com/kyma-project/kyma-environment-broker/common/director/automock"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/logger"
    10  	machineGraphql "github.com/machinebox/graphql"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/mock"
    13  )
    14  
    15  func TestClient_SetLabel(t *testing.T) {
    16  	// given
    17  	var (
    18  		accountID  = "ad568853-ecf3-433a-8638-e53aa6bead5d"
    19  		runtimeID  = "775dc85e-825b-4ddf-abf6-da0dd002b66e"
    20  		labelKey   = "testKey"
    21  		labelValue = "testValue"
    22  	)
    23  
    24  	qc := &mocks.GraphQLClient{}
    25  	cfg := Config{}
    26  
    27  	client := NewDirectorClient(context.Background(), cfg, logger.NewLogDummy())
    28  	client.graphQLClient = qc
    29  
    30  	request := createGraphQLLabelRequest(client, accountID, runtimeID, labelKey, labelValue)
    31  
    32  	qc.On("Run", context.Background(), request, mock.AnythingOfType("*director.runtimeLabelResponse")).Run(func(args mock.Arguments) {
    33  		arg, ok := args.Get(2).(*runtimeLabelResponse)
    34  		if !ok {
    35  			return
    36  		}
    37  		arg.Result = &graphql.Label{
    38  			Key:   labelKey,
    39  			Value: labelValue,
    40  		}
    41  	}).Return(nil)
    42  	defer qc.AssertExpectations(t)
    43  
    44  	// when
    45  	err := client.SetLabel(accountID, runtimeID, labelKey, labelValue)
    46  
    47  	// then
    48  	assert.NoError(t, err)
    49  }
    50  
    51  func createGraphQLRequest(client *Client, accountID, runtimeID string) *machineGraphql.Request {
    52  	query := client.queryProvider.Runtime(runtimeID)
    53  	request := machineGraphql.NewRequest(query)
    54  	request.Header.Add(accountIDKey, accountID)
    55  
    56  	return request
    57  }
    58  
    59  func createGraphQLLabelRequest(client *Client, accountID, runtimeID, key, label string) *machineGraphql.Request {
    60  	query := client.queryProvider.SetRuntimeLabel(runtimeID, key, label)
    61  	request := machineGraphql.NewRequest(query)
    62  	request.Header.Add(accountIDKey, accountID)
    63  
    64  	return request
    65  }