github.com/lulzWill/go-agent@v2.1.2+incompatible/internal_config_test.go (about)

     1  package newrelic
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"os"
     7  	"regexp"
     8  	"strconv"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/lulzWill/go-agent/internal"
    13  	"github.com/lulzWill/go-agent/internal/utilization"
    14  )
    15  
    16  var (
    17  	fixRegex = regexp.MustCompile(`e\+\d+`)
    18  )
    19  
    20  // In Go 1.8 Marshalling of numbers was changed:
    21  // Before: "StackTraceThreshold":5e+08
    22  // After:  "StackTraceThreshold":500000000
    23  func standardizeNumbers(input string) string {
    24  	return fixRegex.ReplaceAllStringFunc(input, func(s string) string {
    25  		n, err := strconv.Atoi(s[2:])
    26  		if nil != err {
    27  			return s
    28  		}
    29  		return strings.Repeat("0", n)
    30  	})
    31  }
    32  
    33  func TestCopyConfigReferenceFieldsPresent(t *testing.T) {
    34  	cfg := NewConfig("my appname", "0123456789012345678901234567890123456789")
    35  	cfg.Labels["zip"] = "zap"
    36  	cfg.ErrorCollector.IgnoreStatusCodes = append(cfg.ErrorCollector.IgnoreStatusCodes, 405)
    37  	cfg.Attributes.Include = append(cfg.Attributes.Include, "1")
    38  	cfg.Attributes.Exclude = append(cfg.Attributes.Exclude, "2")
    39  	cfg.TransactionEvents.Attributes.Include = append(cfg.TransactionEvents.Attributes.Include, "3")
    40  	cfg.TransactionEvents.Attributes.Exclude = append(cfg.TransactionEvents.Attributes.Exclude, "4")
    41  	cfg.ErrorCollector.Attributes.Include = append(cfg.ErrorCollector.Attributes.Include, "5")
    42  	cfg.ErrorCollector.Attributes.Exclude = append(cfg.ErrorCollector.Attributes.Exclude, "6")
    43  	cfg.TransactionTracer.Attributes.Include = append(cfg.TransactionTracer.Attributes.Include, "7")
    44  	cfg.TransactionTracer.Attributes.Exclude = append(cfg.TransactionTracer.Attributes.Exclude, "8")
    45  	cfg.Transport = &http.Transport{}
    46  	cfg.Logger = NewLogger(os.Stdout)
    47  
    48  	cp := copyConfigReferenceFields(cfg)
    49  
    50  	cfg.Labels["zop"] = "zup"
    51  	cfg.ErrorCollector.IgnoreStatusCodes[0] = 201
    52  	cfg.Attributes.Include[0] = "zap"
    53  	cfg.Attributes.Exclude[0] = "zap"
    54  	cfg.TransactionEvents.Attributes.Include[0] = "zap"
    55  	cfg.TransactionEvents.Attributes.Exclude[0] = "zap"
    56  	cfg.ErrorCollector.Attributes.Include[0] = "zap"
    57  	cfg.ErrorCollector.Attributes.Exclude[0] = "zap"
    58  	cfg.TransactionTracer.Attributes.Include[0] = "zap"
    59  	cfg.TransactionTracer.Attributes.Exclude[0] = "zap"
    60  
    61  	expect := internal.CompactJSONString(`[
    62  	{
    63  		"pid":123,
    64  		"language":"go",
    65  		"agent_version":"0.2.2",
    66  		"host":"my-hostname",
    67  		"settings":{
    68  			"AppName":"my appname",
    69  			"Attributes":{"Enabled":true,"Exclude":["2"],"Include":["1"]},
    70  			"CrossApplicationTracer":{"Enabled":true},
    71  			"CustomInsightsEvents":{"Enabled":true},
    72  			"DatastoreTracer":{
    73  				"DatabaseNameReporting":{"Enabled":true},
    74  				"InstanceReporting":{"Enabled":true},
    75  				"QueryParameters":{"Enabled":true},
    76  				"SlowQuery":{
    77  					"Enabled":true,
    78  					"Threshold":10000000
    79  				}
    80  			},
    81  			"Enabled":true,
    82  			"ErrorCollector":{
    83  				"Attributes":{"Enabled":true,"Exclude":["6"],"Include":["5"]},
    84  				"CaptureEvents":true,
    85  				"Enabled":true,
    86  				"IgnoreStatusCodes":[404,405]
    87  			},
    88  			"HighSecurity":false,
    89  			"HostDisplayName":"",
    90  			"Labels":{"zip":"zap"},
    91  			"Logger":"*logger.logFile",
    92  			"RuntimeSampler":{"Enabled":true},
    93  			"SecurityPoliciesToken":"",
    94  			"TransactionEvents":{
    95  				"Attributes":{"Enabled":true,"Exclude":["4"],"Include":["3"]},
    96  				"Enabled":true
    97  			},
    98  			"TransactionTracer":{
    99  				"Attributes":{"Enabled":true,"Exclude":["8"],"Include":["7"]},
   100  				"Enabled":true,
   101  				"SegmentThreshold":2000000,
   102  				"StackTraceThreshold":500000000,
   103  				"Threshold":{
   104  					"Duration":500000000,
   105  					"IsApdexFailing":true
   106  				}
   107  			},
   108  			"Transport":"*http.Transport",
   109  			"Utilization":{
   110  				"BillingHostname":"",
   111  				"DetectAWS":true,
   112  				"DetectAzure":true,
   113  				"DetectDocker":true,
   114  				"DetectGCP":true,
   115  				"DetectPCF":true,
   116  				"LogicalProcessors":0,
   117  				"TotalRAMMIB":0
   118  			}
   119  		},
   120  		"app_name":["my appname"],
   121  		"high_security":false,
   122  		"labels":[{"label_type":"zip","label_value":"zap"}],
   123  		"environment":[
   124  			["runtime.Compiler","comp"],
   125  			["runtime.GOARCH","arch"],
   126  			["runtime.GOOS","goos"],
   127  			["runtime.Version","vers"],
   128  			["runtime.NumCPU",8]
   129  		],
   130  		"identifier":"my appname",
   131  		"utilization":{
   132  			"metadata_version":3,
   133  			"logical_processors":16,
   134  			"total_ram_mib":1024,
   135  			"hostname":"my-hostname"
   136  		},
   137  		"security_policies":{
   138  			"record_sql":{"enabled":false},
   139  			"attributes_include":{"enabled":false},
   140  			"allow_raw_exception_messages":{"enabled":false},
   141  			"custom_events":{"enabled":false},
   142  			"custom_parameters":{"enabled":false}
   143  		}
   144  	}]`)
   145  
   146  	securityPoliciesInput := []byte(`{
   147  		"record_sql":                    { "enabled": false, "required": false },
   148  	        "attributes_include":            { "enabled": false, "required": false },
   149  	        "allow_raw_exception_messages":  { "enabled": false, "required": false },
   150  	        "custom_events":                 { "enabled": false, "required": false },
   151  	        "custom_parameters":             { "enabled": false, "required": false },
   152  	        "custom_instrumentation_editor": { "enabled": false, "required": false },
   153  	        "message_parameters":            { "enabled": false, "required": false },
   154  	        "job_arguments":                 { "enabled": false, "required": false }
   155  	}`)
   156  	var sp internal.SecurityPolicies
   157  	err := json.Unmarshal(securityPoliciesInput, &sp)
   158  	if nil != err {
   159  		t.Fatal(err)
   160  	}
   161  
   162  	js, err := configConnectJSONInternal(cp, 123, &utilization.SampleData, internal.SampleEnvironment, "0.2.2", sp.PointerIfPopulated())
   163  	if nil != err {
   164  		t.Fatal(err)
   165  	}
   166  	out := standardizeNumbers(string(js))
   167  	if out != expect {
   168  		t.Error(out)
   169  	}
   170  }
   171  
   172  func TestCopyConfigReferenceFieldsAbsent(t *testing.T) {
   173  	cfg := NewConfig("my appname", "0123456789012345678901234567890123456789")
   174  	cfg.Labels = nil
   175  	cfg.ErrorCollector.IgnoreStatusCodes = nil
   176  
   177  	cp := copyConfigReferenceFields(cfg)
   178  
   179  	expect := internal.CompactJSONString(`[
   180  	{
   181  		"pid":123,
   182  		"language":"go",
   183  		"agent_version":"0.2.2",
   184  		"host":"my-hostname",
   185  		"settings":{
   186  			"AppName":"my appname",
   187  			"Attributes":{"Enabled":true,"Exclude":null,"Include":null},
   188  			"CrossApplicationTracer":{"Enabled":true},
   189  			"CustomInsightsEvents":{"Enabled":true},
   190  			"DatastoreTracer":{
   191  				"DatabaseNameReporting":{"Enabled":true},
   192  				"InstanceReporting":{"Enabled":true},
   193  				"QueryParameters":{"Enabled":true},
   194  				"SlowQuery":{
   195  					"Enabled":true,
   196  					"Threshold":10000000
   197  				}
   198  			},
   199  			"Enabled":true,
   200  			"ErrorCollector":{
   201  				"Attributes":{"Enabled":true,"Exclude":null,"Include":null},
   202  				"CaptureEvents":true,
   203  				"Enabled":true,
   204  				"IgnoreStatusCodes":null
   205  			},
   206  			"HighSecurity":false,
   207  			"HostDisplayName":"",
   208  			"Labels":null,
   209  			"Logger":null,
   210  			"RuntimeSampler":{"Enabled":true},
   211  			"SecurityPoliciesToken":"",
   212  			"TransactionEvents":{
   213  				"Attributes":{"Enabled":true,"Exclude":null,"Include":null},
   214  				"Enabled":true
   215  			},
   216  			"TransactionTracer":{
   217  				"Attributes":{"Enabled":true,"Exclude":null,"Include":null},
   218  				"Enabled":true,
   219  				"SegmentThreshold":2000000,
   220  				"StackTraceThreshold":500000000,
   221  				"Threshold":{
   222  					"Duration":500000000,
   223  					"IsApdexFailing":true
   224  				}
   225  			},
   226  			"Transport":null,
   227  			"Utilization":{
   228  				"BillingHostname":"",
   229  				"DetectAWS":true,
   230  				"DetectAzure":true,
   231  				"DetectDocker":true,
   232  				"DetectGCP":true,
   233  				"DetectPCF":true,
   234  				"LogicalProcessors":0,
   235  				"TotalRAMMIB":0
   236  			}
   237  		},
   238  		"app_name":["my appname"],
   239  		"high_security":false,
   240  		"environment":[
   241  			["runtime.Compiler","comp"],
   242  			["runtime.GOARCH","arch"],
   243  			["runtime.GOOS","goos"],
   244  			["runtime.Version","vers"],
   245  			["runtime.NumCPU",8]
   246  		],
   247  		"identifier":"my appname",
   248  		"utilization":{
   249  			"metadata_version":3,
   250  			"logical_processors":16,
   251  			"total_ram_mib":1024,
   252  			"hostname":"my-hostname"
   253  		}
   254  	}]`)
   255  
   256  	js, err := configConnectJSONInternal(cp, 123, &utilization.SampleData, internal.SampleEnvironment, "0.2.2", nil)
   257  	if nil != err {
   258  		t.Fatal(err)
   259  	}
   260  	out := standardizeNumbers(string(js))
   261  	if out != expect {
   262  		t.Error(string(js))
   263  	}
   264  }
   265  
   266  func TestValidate(t *testing.T) {
   267  	c := Config{
   268  		License: "0123456789012345678901234567890123456789",
   269  		AppName: "my app",
   270  		Enabled: true,
   271  	}
   272  	if err := c.Validate(); nil != err {
   273  		t.Error(err)
   274  	}
   275  	c = Config{
   276  		License: "",
   277  		AppName: "my app",
   278  		Enabled: true,
   279  	}
   280  	if err := c.Validate(); err != errLicenseLen {
   281  		t.Error(err)
   282  	}
   283  	c = Config{
   284  		License: "",
   285  		AppName: "my app",
   286  		Enabled: false,
   287  	}
   288  	if err := c.Validate(); nil != err {
   289  		t.Error(err)
   290  	}
   291  	c = Config{
   292  		License: "wronglength",
   293  		AppName: "my app",
   294  		Enabled: true,
   295  	}
   296  	if err := c.Validate(); err != errLicenseLen {
   297  		t.Error(err)
   298  	}
   299  	c = Config{
   300  		License: "0123456789012345678901234567890123456789",
   301  		AppName: "too;many;app;names",
   302  		Enabled: true,
   303  	}
   304  	if err := c.Validate(); err != errAppNameLimit {
   305  		t.Error(err)
   306  	}
   307  	c = Config{
   308  		License: "0123456789012345678901234567890123456789",
   309  		AppName: "",
   310  		Enabled: true,
   311  	}
   312  	if err := c.Validate(); err != errAppNameMissing {
   313  		t.Error(err)
   314  	}
   315  	c = Config{
   316  		License: "0123456789012345678901234567890123456789",
   317  		AppName: "",
   318  		Enabled: false,
   319  	}
   320  	if err := c.Validate(); err != nil {
   321  		t.Error(err)
   322  	}
   323  	c = Config{
   324  		License:      "0123456789012345678901234567890123456789",
   325  		AppName:      "my app",
   326  		Enabled:      true,
   327  		HighSecurity: true,
   328  	}
   329  	if err := c.Validate(); err != nil {
   330  		t.Error(err)
   331  	}
   332  }
   333  
   334  func TestValidateWithPoliciesToken(t *testing.T) {
   335  	c := Config{
   336  		License:               "0123456789012345678901234567890123456789",
   337  		AppName:               "my app",
   338  		Enabled:               true,
   339  		HighSecurity:          true,
   340  		SecurityPoliciesToken: "0123456789",
   341  	}
   342  	if err := c.Validate(); err != errHighSecurityWithSecurityPolicies {
   343  		t.Error(err)
   344  	}
   345  	c = Config{
   346  		License:               "0123456789012345678901234567890123456789",
   347  		AppName:               "my app",
   348  		Enabled:               true,
   349  		SecurityPoliciesToken: "0123456789",
   350  	}
   351  	if err := c.Validate(); err != nil {
   352  		t.Error(err)
   353  	}
   354  }