github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/clients/pkg/promtail/targets/lokipush/pushtargetmanager_test.go (about)

     1  package lokipush
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/weaveworks/common/server"
     7  
     8  	"github.com/grafana/loki/clients/pkg/promtail/scrapeconfig"
     9  )
    10  
    11  func Test_validateJobName(t *testing.T) {
    12  	tests := []struct {
    13  		name    string
    14  		configs []scrapeconfig.Config
    15  		// Only validated against the first job in the provided scrape configs
    16  		expectedJob string
    17  		wantErr     bool
    18  	}{
    19  		{
    20  			name: "valid with spaces removed",
    21  			configs: []scrapeconfig.Config{
    22  				{
    23  					JobName: "jobby job job",
    24  					PushConfig: &scrapeconfig.PushTargetConfig{
    25  						Server: server.Config{},
    26  					},
    27  				},
    28  			},
    29  			wantErr:     false,
    30  			expectedJob: "jobby_job_job",
    31  		},
    32  		{
    33  			name: "missing job",
    34  			configs: []scrapeconfig.Config{
    35  				{
    36  					PushConfig: &scrapeconfig.PushTargetConfig{
    37  						Server: server.Config{},
    38  					},
    39  				},
    40  			},
    41  			wantErr: true,
    42  		},
    43  		{
    44  			name: "duplicate job",
    45  			configs: []scrapeconfig.Config{
    46  				{
    47  					JobName: "job1",
    48  					PushConfig: &scrapeconfig.PushTargetConfig{
    49  						Server: server.Config{},
    50  					},
    51  				},
    52  				{
    53  					JobName: "job1",
    54  					PushConfig: &scrapeconfig.PushTargetConfig{
    55  						Server: server.Config{},
    56  					},
    57  				},
    58  			},
    59  			wantErr: true,
    60  		},
    61  	}
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			err := validateJobName(tt.configs)
    65  			if (err != nil) != tt.wantErr {
    66  				t.Errorf("validateJobName() error = %v, wantErr %v", err, tt.wantErr)
    67  				return
    68  			}
    69  			if !tt.wantErr {
    70  				if tt.configs[0].JobName != tt.expectedJob {
    71  					t.Errorf("Expected to find a job with name %v but did not find it", tt.expectedJob)
    72  					return
    73  				}
    74  			}
    75  		})
    76  	}
    77  }