github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/gadgettracermanager/gadgettracermanager_test.go (about)

     1  // Copyright 2019-2021 The Inspektor Gadget authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gadgettracermanager
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	containercollection "github.com/inspektor-gadget/inspektor-gadget/pkg/container-collection"
    22  	"github.com/inspektor-gadget/inspektor-gadget/pkg/types"
    23  )
    24  
    25  func TestTracer(t *testing.T) {
    26  	g, err := NewServer(&Conf{NodeName: "fake-node", HookMode: "none", TestOnly: true})
    27  	if err != nil {
    28  		t.Fatalf("Failed to create new server: %v", err)
    29  	}
    30  
    31  	// Add 3 Tracers
    32  	for i := 0; i < 3; i++ {
    33  		err := g.AddTracer(
    34  			fmt.Sprintf("my_tracer_id%d", i),
    35  			containercollection.ContainerSelector{
    36  				K8s: containercollection.K8sSelector{
    37  					BasicK8sMetadata: types.BasicK8sMetadata{
    38  						Namespace: fmt.Sprintf("this-namespace%d", i),
    39  					},
    40  				},
    41  			},
    42  		)
    43  		if err != nil {
    44  			t.Fatalf("Failed to add tracer: %v", err)
    45  		}
    46  	}
    47  
    48  	if g.tracerCollection.TracerCount() != 3 {
    49  		t.Fatalf("Error while checking tracers: len %d", g.tracerCollection.TracerCount())
    50  	}
    51  
    52  	// Check error on duplicate tracer
    53  	err = g.AddTracer(
    54  		fmt.Sprintf("my_tracer_id%d", 0),
    55  		containercollection.ContainerSelector{
    56  			K8s: containercollection.K8sSelector{
    57  				BasicK8sMetadata: types.BasicK8sMetadata{
    58  					Namespace: fmt.Sprintf("this-namespace%d", 0),
    59  				},
    60  			},
    61  		},
    62  	)
    63  	if err == nil {
    64  		t.Fatal("Error while trying to add a duplicate tracer: duplicate not detected")
    65  	}
    66  
    67  	// Remove 1 Tracer
    68  	err = g.RemoveTracer(fmt.Sprintf("my_tracer_id%d", 1))
    69  	if err != nil {
    70  		t.Fatalf("Failed to remove tracer: %v", err)
    71  	}
    72  
    73  	// Remove non-existent Tracer
    74  	err = g.RemoveTracer(fmt.Sprintf("my_tracer_id%d", 99))
    75  	if err == nil {
    76  		t.Fatal("Error while removing non-existent tracer: no error detected")
    77  	}
    78  
    79  	// Check content
    80  	if g.tracerCollection.TracerCount() != 2 {
    81  		t.Fatalf("Error while checking tracers: len %d", g.tracerCollection.TracerCount())
    82  	}
    83  	if !g.tracerCollection.TracerExists("my_tracer_id0") {
    84  		t.Fatalf("Error while checking tracer %s: not found", "my_tracer_id0")
    85  	}
    86  	if !g.tracerCollection.TracerExists("my_tracer_id2") {
    87  		t.Fatalf("Error while checking tracer %s: not found", "my_tracer_id2")
    88  	}
    89  }