github.com/jlmeeker/kismatic@v1.10.1-0.20180612190640-57f9005a1f1a/pkg/inspector/check/distro_test.go (about)

     1  package check
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestDetectDistroFromOSRelease(t *testing.T) {
     9  	tests := []struct {
    10  		osReleaseFile  string
    11  		expectedDistro Distro
    12  		expectErr      bool
    13  	}{
    14  		{
    15  			osReleaseFile:  centos7ReleaseFile,
    16  			expectedDistro: CentOS,
    17  			expectErr:      false,
    18  		},
    19  		{
    20  			osReleaseFile:  rhel7ReleaseFile,
    21  			expectedDistro: RHEL,
    22  			expectErr:      false,
    23  		},
    24  		{
    25  			osReleaseFile:  ubuntu1604ReleaseFile,
    26  			expectedDistro: Ubuntu,
    27  			expectErr:      false,
    28  		},
    29  		{
    30  			osReleaseFile:  "",
    31  			expectedDistro: Unsupported,
    32  			expectErr:      true,
    33  		},
    34  		{
    35  			osReleaseFile:  missingIDFieldOSReleaseFile,
    36  			expectedDistro: Unsupported,
    37  			expectErr:      true,
    38  		},
    39  	}
    40  
    41  	for _, test := range tests {
    42  		d, err := detectDistroFromOSRelease(strings.NewReader(test.osReleaseFile))
    43  		if test.expectErr && err == nil {
    44  			t.Error("expected an error, but didn't get one")
    45  		}
    46  
    47  		if !test.expectErr && err != nil {
    48  			t.Errorf("unexpected error occurred when running test: %v", err)
    49  		}
    50  
    51  		if d != test.expectedDistro {
    52  			t.Errorf("failed to detect distro. expected %s, found %s", test.expectedDistro, d)
    53  		}
    54  	}
    55  }
    56  
    57  var centos7ReleaseFile = `NAME="CentOS Linux"
    58  VERSION="7 (Core)"
    59  ID="centos"
    60  ID_LIKE="rhel fedora"
    61  VERSION_ID="7"
    62  PRETTY_NAME="CentOS Linux 7 (Core)"
    63  ANSI_COLOR="0;31"
    64  CPE_NAME="cpe:/o:centos:centos:7"
    65  HOME_URL="https://www.centos.org/"
    66  BUG_REPORT_URL="https://bugs.centos.org/"
    67  
    68  CENTOS_MANTISBT_PROJECT="CentOS-7"
    69  CENTOS_MANTISBT_PROJECT_VERSION="7"
    70  REDHAT_SUPPORT_PRODUCT="centos"
    71  REDHAT_SUPPORT_PRODUCT_VERSION="7"`
    72  
    73  var rhel7ReleaseFile = `NAME="Red Hat Enterprise Linux Server"
    74  VERSION="7.2 (Maipo)"
    75  ID="rhel"
    76  ID_LIKE="fedora"
    77  VERSION_ID="7.2"
    78  PRETTY_NAME="Red Hat Enterprise Linux Server 7.2 (Maipo)"
    79  ANSI_COLOR="0;31"
    80  CPE_NAME="cpe:/o:redhat:enterprise_linux:7.2:GA:server"
    81  HOME_URL="https://www.redhat.com/"
    82  BUG_REPORT_URL="https://bugzilla.redhat.com/"
    83  
    84  REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7"
    85  REDHAT_BUGZILLA_PRODUCT_VERSION=7.2
    86  REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
    87  REDHAT_SUPPORT_PRODUCT_VERSION="7.2"`
    88  
    89  var ubuntu1604ReleaseFile = `NAME="Ubuntu"
    90  VERSION="16.04.1 LTS (Xenial Xerus)"
    91  ID=ubuntu
    92  ID_LIKE=debian
    93  PRETTY_NAME="Ubuntu 16.04.1 LTS"
    94  VERSION_ID="16.04"
    95  HOME_URL="http://www.ubuntu.com/"
    96  SUPPORT_URL="http://help.ubuntu.com/"
    97  BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
    98  UBUNTU_CODENAME=xenial`
    99  
   100  var missingIDFieldOSReleaseFile = `NAME="Ubuntu"
   101  VERSION="16.04.1 LTS (Xenial Xerus)"
   102  ID_LIKE=debian
   103  PRETTY_NAME="Ubuntu 16.04.1 LTS"
   104  VERSION_ID="16.04"
   105  HOME_URL="http://www.ubuntu.com/"
   106  SUPPORT_URL="http://help.ubuntu.com/"
   107  BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
   108  UBUNTU_CODENAME=xenial`