github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/rollout-service/pkg/notifier/notifier_test.go (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  
    17  package notifier
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	argoapplication "github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
    24  	argoappv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
    25  
    26  	"google.golang.org/grpc"
    27  )
    28  
    29  type mockApplicationClient struct {
    30  	requests chan *argoapplication.ApplicationQuery
    31  }
    32  
    33  func (m *mockApplicationClient) Get(ctx context.Context, in *argoapplication.ApplicationQuery, opts ...grpc.CallOption) (*argoappv1.Application, error) {
    34  	m.requests <- in
    35  	return nil, nil
    36  }
    37  
    38  func TestNotifier(t *testing.T) {
    39  	tcs := []struct {
    40  		Name             string
    41  		ConcurrencyLimit int
    42  	}{
    43  		{
    44  			Name:             "sends requests in parallel",
    45  			ConcurrencyLimit: 10,
    46  		},
    47  	}
    48  
    49  	for _, tc := range tcs {
    50  		tc := tc
    51  		t.Run(tc.Name, func(t *testing.T) {
    52  			ctx := context.Background()
    53  			// chan without capacity will block all requests
    54  			ch := make(chan *argoapplication.ApplicationQuery)
    55  			ma := &mockApplicationClient{ch}
    56  			nf := New(ma, tc.ConcurrencyLimit)
    57  			for i := 0; i < tc.ConcurrencyLimit; i = i + 1 {
    58  				nf.NotifyArgoCd(ctx, "foo", "bar")
    59  			}
    60  
    61  			for i := 0; i < tc.ConcurrencyLimit; i = i + 1 {
    62  				in := <-ch
    63  				if *in.Name != "foo-bar" {
    64  					t.Errorf("expected application %q, but got %q", "foo-bar", *in.Name)
    65  				}
    66  				if *in.Refresh != "normal" {
    67  					t.Errorf("expected referesh type %q, but got %q", "normal", *in.Refresh)
    68  				}
    69  			}
    70  		})
    71  
    72  	}
    73  }