github.com/newrelic/go-agent@v3.26.0+incompatible/internal/sysinfo/docker_test.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package sysinfo
     5  
     6  import (
     7  	"bytes"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/newrelic/go-agent/internal/crossagent"
    12  )
    13  
    14  func TestDockerIDCrossAgent(t *testing.T) {
    15  	var testCases []struct {
    16  		File string `json:"filename"`
    17  		ID   string `json:"containerId"`
    18  	}
    19  
    20  	dir := "docker_container_id"
    21  	err := crossagent.ReadJSON(filepath.Join(dir, "cases.json"), &testCases)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	for _, test := range testCases {
    27  		file := filepath.Join(dir, test.File)
    28  		input, err := crossagent.ReadFile(file)
    29  		if err != nil {
    30  			t.Error(err)
    31  			continue
    32  		}
    33  
    34  		got, _ := parseDockerID(bytes.NewReader(input))
    35  		if got != test.ID {
    36  			t.Errorf("%s != %s", got, test.ID)
    37  		}
    38  	}
    39  }
    40  
    41  func TestDockerIDValidation(t *testing.T) {
    42  	err := validateDockerID("baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1239")
    43  	if nil != err {
    44  		t.Error("Validation should pass with a 64-character hex string.")
    45  	}
    46  	err = validateDockerID("39ffbba")
    47  	if nil == err {
    48  		t.Error("Validation should have failed with short string.")
    49  	}
    50  	err = validateDockerID("z000000000000000000000000000000000000000000000000100000000000000")
    51  	if nil == err {
    52  		t.Error("Validation should have failed with non-hex characters.")
    53  	}
    54  }