github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/acceptor/docker_test.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  package acceptor_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/instana/testify/assert"
    10  	"github.com/mier85/go-sensor/acceptor"
    11  	"github.com/mier85/go-sensor/docker"
    12  )
    13  
    14  func TestNewDockerPluginPayload(t *testing.T) {
    15  	data := acceptor.DockerData{
    16  		ID: "docker1",
    17  	}
    18  
    19  	assert.Equal(t, acceptor.PluginPayload{
    20  		Name:     "com.instana.plugin.docker",
    21  		EntityID: "id1",
    22  		Data:     data,
    23  	}, acceptor.NewDockerPluginPayload("id1", data))
    24  }
    25  
    26  func TestNewDockerNetworkAggregatedStatsDelta(t *testing.T) {
    27  	stats := map[string]docker.ContainerNetworkStats{
    28  		"eth0": {
    29  			RxBytes:   1,
    30  			RxDropped: 10,
    31  			RxErrors:  100,
    32  			RxPackets: 1000,
    33  			TxBytes:   10000,
    34  			TxDropped: 100000,
    35  			TxErrors:  1000000,
    36  			TxPackets: 10000000,
    37  		},
    38  		"eth1": {
    39  			RxBytes:   2,
    40  			RxDropped: 20,
    41  			RxErrors:  200,
    42  			RxPackets: 2000,
    43  			TxBytes:   20000,
    44  			TxDropped: 200000,
    45  			TxErrors:  2000000,
    46  			TxPackets: 20000000,
    47  		},
    48  		"eth2": {
    49  			RxBytes:   3,
    50  			RxDropped: 30,
    51  			RxErrors:  300,
    52  			RxPackets: 3000,
    53  			TxBytes:   30000,
    54  			TxDropped: 300000,
    55  			TxErrors:  3000000,
    56  			TxPackets: 30000000,
    57  		},
    58  	}
    59  
    60  	t.Run("equal", func(t *testing.T) {
    61  		assert.Nil(t, acceptor.NewDockerNetworkAggregatedStatsDelta(stats, stats))
    62  	})
    63  
    64  	t.Run("increase", func(t *testing.T) {
    65  		assert.Equal(t,
    66  			&acceptor.DockerNetworkAggregatedStatsDelta{
    67  				Rx: &acceptor.DockerNetworkStatsDelta{
    68  					Bytes:   6,
    69  					Dropped: 60,
    70  					Errors:  600,
    71  					Packets: 6000,
    72  				},
    73  				Tx: &acceptor.DockerNetworkStatsDelta{
    74  					Bytes:   60000,
    75  					Dropped: 600000,
    76  					Errors:  6000000,
    77  					Packets: 60000000,
    78  				},
    79  			},
    80  			acceptor.NewDockerNetworkAggregatedStatsDelta(nil, stats),
    81  		)
    82  	})
    83  
    84  	t.Run("decrease", func(t *testing.T) {
    85  		assert.Equal(t,
    86  			&acceptor.DockerNetworkAggregatedStatsDelta{
    87  				Rx: &acceptor.DockerNetworkStatsDelta{
    88  					Bytes:   -6,
    89  					Dropped: -60,
    90  					Errors:  -600,
    91  					Packets: -6000,
    92  				},
    93  				Tx: &acceptor.DockerNetworkStatsDelta{
    94  					Bytes:   -60000,
    95  					Dropped: -600000,
    96  					Errors:  -6000000,
    97  					Packets: -60000000,
    98  				},
    99  			},
   100  			acceptor.NewDockerNetworkAggregatedStatsDelta(stats, nil),
   101  		)
   102  	})
   103  }
   104  
   105  func TestNewDockerCPUStatsDelta(t *testing.T) {
   106  	stats := docker.ContainerCPUStats{
   107  		Usage: docker.CPUUsageStats{
   108  			Total:  1,
   109  			User:   10,
   110  			Kernel: 100,
   111  		},
   112  		Throttling: docker.CPUThrottlingStats{
   113  			Periods: 1000,
   114  			Time:    10000,
   115  		},
   116  		System:     100000,
   117  		OnlineCPUs: 16,
   118  	}
   119  
   120  	t.Run("equal", func(t *testing.T) {
   121  		assert.Nil(t, acceptor.NewDockerCPUStatsDelta(stats, stats))
   122  	})
   123  
   124  	t.Run("increase", func(t *testing.T) {
   125  		assert.Equal(t,
   126  			&acceptor.DockerCPUStatsDelta{
   127  				Total:           0.00016,
   128  				User:            0.0016,
   129  				System:          0.016,
   130  				ThrottlingCount: 1000,
   131  				ThrottlingTime:  10000,
   132  			},
   133  			acceptor.NewDockerCPUStatsDelta(docker.ContainerCPUStats{}, stats),
   134  		)
   135  	})
   136  
   137  	t.Run("decrease", func(t *testing.T) {
   138  		assert.Equal(t,
   139  			&acceptor.DockerCPUStatsDelta{
   140  				Total:           0,
   141  				User:            0,
   142  				System:          0,
   143  				ThrottlingCount: -1000,
   144  				ThrottlingTime:  -10000,
   145  			},
   146  			acceptor.NewDockerCPUStatsDelta(stats, docker.ContainerCPUStats{}),
   147  		)
   148  	})
   149  }
   150  
   151  func TestNewDockerMemoryStatsDelta(t *testing.T) {
   152  	stats := docker.ContainerMemoryStats{
   153  		Stats: docker.MemoryStats{
   154  			ActiveAnon:   1,
   155  			ActiveFile:   10,
   156  			InactiveAnon: 100,
   157  			InactiveFile: 1000,
   158  			TotalRss:     10000,
   159  			TotalCache:   100000,
   160  		},
   161  		MaxUsage: 1000000,
   162  		Usage:    10000000,
   163  		Limit:    100000000,
   164  	}
   165  
   166  	t.Run("equal", func(t *testing.T) {
   167  		assert.Nil(t, acceptor.NewDockerMemoryStatsUpdate(stats, stats))
   168  	})
   169  
   170  	t.Run("changed", func(t *testing.T) {
   171  		assert.Equal(t,
   172  			&acceptor.DockerMemoryStatsUpdate{
   173  				ActiveAnon:   &stats.Stats.ActiveAnon,
   174  				ActiveFile:   &stats.Stats.ActiveFile,
   175  				InactiveAnon: &stats.Stats.InactiveAnon,
   176  				InactiveFile: &stats.Stats.InactiveFile,
   177  				TotalRss:     &stats.Stats.TotalRss,
   178  				TotalCache:   &stats.Stats.TotalCache,
   179  				MaxUsage:     &stats.MaxUsage,
   180  				Usage:        &stats.Usage,
   181  				Limit:        &stats.Limit,
   182  			},
   183  			acceptor.NewDockerMemoryStatsUpdate(docker.ContainerMemoryStats{}, stats),
   184  		)
   185  	})
   186  
   187  	t.Run("changed some", func(t *testing.T) {
   188  		assert.Equal(t,
   189  			&acceptor.DockerMemoryStatsUpdate{
   190  				ActiveFile:   &stats.Stats.ActiveFile,
   191  				InactiveFile: &stats.Stats.InactiveFile,
   192  				TotalCache:   &stats.Stats.TotalCache,
   193  				MaxUsage:     &stats.MaxUsage,
   194  				Limit:        &stats.Limit,
   195  			},
   196  			acceptor.NewDockerMemoryStatsUpdate(docker.ContainerMemoryStats{
   197  				Stats: docker.MemoryStats{
   198  					ActiveAnon:   1,
   199  					ActiveFile:   20,
   200  					InactiveAnon: 100,
   201  					InactiveFile: 2000,
   202  					TotalRss:     10000,
   203  					TotalCache:   200000,
   204  				},
   205  				MaxUsage: 2000000,
   206  				Usage:    10000000,
   207  				Limit:    200000000,
   208  			}, stats),
   209  		)
   210  	})
   211  }
   212  
   213  func TestNewDockerBlockIOStatsDelta(t *testing.T) {
   214  	stats := docker.ContainerBlockIOStats{
   215  		ServiceBytes: []docker.BlockIOOpStats{
   216  			{Operation: docker.BlockIOReadOp, Value: 1},
   217  			{Operation: docker.BlockIOWriteOp, Value: 10},
   218  			{Operation: docker.BlockIOReadOp, Value: 100},
   219  		},
   220  	}
   221  
   222  	t.Run("equal", func(t *testing.T) {
   223  		assert.Nil(t, acceptor.NewDockerBlockIOStatsDelta(stats, stats))
   224  	})
   225  
   226  	t.Run("increase", func(t *testing.T) {
   227  		assert.Equal(t,
   228  			&acceptor.DockerBlockIOStatsDelta{
   229  				Read:  101,
   230  				Write: 10,
   231  			},
   232  			acceptor.NewDockerBlockIOStatsDelta(docker.ContainerBlockIOStats{}, stats),
   233  		)
   234  	})
   235  
   236  	t.Run("decrease", func(t *testing.T) {
   237  		assert.Equal(t,
   238  			&acceptor.DockerBlockIOStatsDelta{
   239  				Read:  -101,
   240  				Write: -10,
   241  			},
   242  			acceptor.NewDockerBlockIOStatsDelta(stats, docker.ContainerBlockIOStats{}),
   243  		)
   244  	})
   245  
   246  	t.Run("equal, shuffled ops", func(t *testing.T) {
   247  		assert.Nil(t, acceptor.NewDockerBlockIOStatsDelta(stats, docker.ContainerBlockIOStats{
   248  			ServiceBytes: []docker.BlockIOOpStats{
   249  				{Operation: docker.BlockIOReadOp, Value: 100},
   250  				{Operation: docker.BlockIOReadOp, Value: 1},
   251  				{Operation: docker.BlockIOWriteOp, Value: 10},
   252  			},
   253  		}))
   254  	})
   255  }