github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrmysql/nrmysql_test.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package nrmysql
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/go-sql-driver/mysql"
    10  	newrelic "github.com/newrelic/go-agent"
    11  )
    12  
    13  func TestParseDSN(t *testing.T) {
    14  	testcases := []struct {
    15  		dsn             string
    16  		expHost         string
    17  		expPortPathOrID string
    18  		expDatabaseName string
    19  	}{
    20  		// examples from https://github.com/go-sql-driver/mysql README
    21  		{
    22  			dsn:             "user@unix(/path/to/socket)/dbname",
    23  			expHost:         "localhost",
    24  			expPortPathOrID: "/path/to/socket",
    25  			expDatabaseName: "dbname",
    26  		},
    27  		{
    28  			dsn:             "root:pw@unix(/tmp/mysql.sock)/myDatabase?loc=Local",
    29  			expHost:         "localhost",
    30  			expPortPathOrID: "/tmp/mysql.sock",
    31  			expDatabaseName: "myDatabase",
    32  		},
    33  		{
    34  			dsn:             "user:password@tcp(localhost:5555)/dbname?tls=skip-verify&autocommit=true",
    35  			expHost:         "localhost",
    36  			expPortPathOrID: "5555",
    37  			expDatabaseName: "dbname",
    38  		},
    39  		{
    40  			dsn:             "user:password@/dbname?sql_mode=TRADITIONAL",
    41  			expHost:         "127.0.0.1",
    42  			expPortPathOrID: "3306",
    43  			expDatabaseName: "dbname",
    44  		},
    45  		{
    46  			dsn:             "user:password@tcp([de:ad:be:ef::ca:fe]:80)/dbname?timeout=90s&collation=utf8mb4_unicode_ci",
    47  			expHost:         "de:ad:be:ef::ca:fe",
    48  			expPortPathOrID: "80",
    49  			expDatabaseName: "dbname",
    50  		},
    51  		{
    52  			dsn:             "id:password@tcp(your-amazonaws-uri.com:3306)/dbname",
    53  			expHost:         "your-amazonaws-uri.com",
    54  			expPortPathOrID: "3306",
    55  			expDatabaseName: "dbname",
    56  		},
    57  		{
    58  			dsn:             "user@cloudsql(project-id:instance-name)/dbname",
    59  			expHost:         "project-id:instance-name",
    60  			expPortPathOrID: "",
    61  			expDatabaseName: "dbname",
    62  		},
    63  		{
    64  			dsn:             "user@cloudsql(project-id:regionname:instance-name)/dbname",
    65  			expHost:         "project-id:regionname:instance-name",
    66  			expPortPathOrID: "",
    67  			expDatabaseName: "dbname",
    68  		},
    69  		{
    70  			dsn:             "user:password@tcp/dbname?charset=utf8mb4,utf8&sys_var=esc%40ped",
    71  			expHost:         "127.0.0.1",
    72  			expPortPathOrID: "3306",
    73  			expDatabaseName: "dbname",
    74  		},
    75  		{
    76  			dsn:             "user:password@/dbname",
    77  			expHost:         "127.0.0.1",
    78  			expPortPathOrID: "3306",
    79  			expDatabaseName: "dbname",
    80  		},
    81  		{
    82  			dsn:             "user:password@/",
    83  			expHost:         "127.0.0.1",
    84  			expPortPathOrID: "3306",
    85  			expDatabaseName: "",
    86  		},
    87  		{
    88  			dsn:             "this is not a dsn",
    89  			expHost:         "",
    90  			expPortPathOrID: "",
    91  			expDatabaseName: "",
    92  		},
    93  	}
    94  
    95  	for _, test := range testcases {
    96  		s := &newrelic.DatastoreSegment{}
    97  		parseDSN(s, test.dsn)
    98  		if test.expHost != s.Host {
    99  			t.Errorf(`incorrect host, expected="%s", actual="%s"`, test.expHost, s.Host)
   100  		}
   101  		if test.expPortPathOrID != s.PortPathOrID {
   102  			t.Errorf(`incorrect port path or id, expected="%s", actual="%s"`, test.expPortPathOrID, s.PortPathOrID)
   103  		}
   104  		if test.expDatabaseName != s.DatabaseName {
   105  			t.Errorf(`incorrect database name, expected="%s", actual="%s"`, test.expDatabaseName, s.DatabaseName)
   106  		}
   107  	}
   108  }
   109  
   110  func TestParseConfig(t *testing.T) {
   111  	testcases := []struct {
   112  		cfgNet          string
   113  		cfgAddr         string
   114  		cfgDBName       string
   115  		expHost         string
   116  		expPortPathOrID string
   117  		expDatabaseName string
   118  	}{
   119  		{
   120  			cfgDBName:       "mydb",
   121  			expDatabaseName: "mydb",
   122  		},
   123  		{
   124  			cfgNet:          "unixgram",
   125  			cfgAddr:         "/path/to/my/sock",
   126  			expHost:         "localhost",
   127  			expPortPathOrID: "/path/to/my/sock",
   128  		},
   129  		{
   130  			cfgNet:          "unixpacket",
   131  			cfgAddr:         "/path/to/my/sock",
   132  			expHost:         "localhost",
   133  			expPortPathOrID: "/path/to/my/sock",
   134  		},
   135  		{
   136  			cfgNet:          "udp",
   137  			cfgAddr:         "[fe80::1%lo0]:53",
   138  			expHost:         "fe80::1%lo0",
   139  			expPortPathOrID: "53",
   140  		},
   141  		{
   142  			cfgNet:          "tcp",
   143  			cfgAddr:         ":80",
   144  			expHost:         "localhost",
   145  			expPortPathOrID: "80",
   146  		},
   147  		{
   148  			cfgNet:          "ip4:1",
   149  			cfgAddr:         "192.0.2.1",
   150  			expHost:         "192.0.2.1",
   151  			expPortPathOrID: "",
   152  		},
   153  		{
   154  			cfgNet:          "tcp6",
   155  			cfgAddr:         "golang.org:http",
   156  			expHost:         "golang.org",
   157  			expPortPathOrID: "http",
   158  		},
   159  		{
   160  			cfgNet:          "ip6:ipv6-icmp",
   161  			cfgAddr:         "2001:db8::1",
   162  			expHost:         "2001:db8::1",
   163  			expPortPathOrID: "",
   164  		},
   165  	}
   166  
   167  	for _, test := range testcases {
   168  		s := &newrelic.DatastoreSegment{}
   169  		cfg := &mysql.Config{
   170  			Net:    test.cfgNet,
   171  			Addr:   test.cfgAddr,
   172  			DBName: test.cfgDBName,
   173  		}
   174  		parseConfig(s, cfg)
   175  		if test.expHost != s.Host {
   176  			t.Errorf(`incorrect host, expected="%s", actual="%s"`, test.expHost, s.Host)
   177  		}
   178  		if test.expPortPathOrID != s.PortPathOrID {
   179  			t.Errorf(`incorrect port path or id, expected="%s", actual="%s"`, test.expPortPathOrID, s.PortPathOrID)
   180  		}
   181  		if test.expDatabaseName != s.DatabaseName {
   182  			t.Errorf(`incorrect database name, expected="%s", actual="%s"`, test.expDatabaseName, s.DatabaseName)
   183  		}
   184  	}
   185  }