github.com/openshift/installer@v1.4.17/pkg/gather/service/analyze_test.go (about)

     1  package service
     2  
     3  import (
     4  	"archive/tar"
     5  	"bytes"
     6  	"compress/gzip"
     7  	"testing"
     8  
     9  	"github.com/sirupsen/logrus"
    10  	"github.com/sirupsen/logrus/hooks/test"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func generateSuccessOutput(stage string) string {
    15  	return `[
    16  {"phase":"service start"},
    17  {"phase":"stage start", "stage":"` + stage + `"},
    18  {"phase":"stage end", "stage":"` + stage + `", "result":"success"},
    19  {"phase":"service end", "result":"success"}
    20  ]`
    21  }
    22  
    23  func generateFailureOutput(stage string) string {
    24  	return `[
    25  {"phase":"service start"},
    26  {"phase":"stage start", "stage":"` + stage + `"},
    27  {"phase":"stage end", "stage":"` + stage + `", "result":"failure", "errorMessage":"Line 1\nLine 2\nLine 3"}
    28  ]`
    29  }
    30  
    31  func failedReleaseImage() []logrus.Entry {
    32  	return []logrus.Entry{
    33  		{Level: logrus.ErrorLevel, Message: "The bootstrap machine failed to download the release image"},
    34  		{Level: logrus.InfoLevel, Message: "Line 1"},
    35  		{Level: logrus.InfoLevel, Message: "Line 2"},
    36  		{Level: logrus.InfoLevel, Message: "Line 3"},
    37  	}
    38  }
    39  
    40  func failedURLChecks() []logrus.Entry {
    41  	return []logrus.Entry{
    42  		{Level: logrus.InfoLevel, Message: "Line 1"},
    43  		{Level: logrus.InfoLevel, Message: "Line 2"},
    44  		{Level: logrus.InfoLevel, Message: "Line 3"},
    45  	}
    46  }
    47  
    48  func TestAnalyzeGatherBundle(t *testing.T) {
    49  	cases := []struct {
    50  		name           string
    51  		files          map[string]string
    52  		expectedOutput []logrus.Entry
    53  	}{
    54  		{
    55  			name: "no files",
    56  			expectedOutput: []logrus.Entry{
    57  				{Level: logrus.ErrorLevel, Message: "Invalid log bundle or the bootstrap machine could not be reached and bootstrap logs were not collected"},
    58  			},
    59  		},
    60  		{
    61  			name: "release-image not started",
    62  			files: map[string]string{
    63  				"log-bundle/bootstrap/services/release-image.json": "[]",
    64  			},
    65  			expectedOutput: []logrus.Entry{
    66  				{Level: logrus.ErrorLevel, Message: "The bootstrap machine did not execute the release-image.service systemd unit"},
    67  			},
    68  		},
    69  		{
    70  			name: "bootkube not started",
    71  			files: map[string]string{
    72  				"log-bundle/bootstrap/services/release-image.json": generateSuccessOutput("pull-release-image"),
    73  				"log-bundle/bootstrap/services/bootkube.json":      "[]",
    74  			},
    75  			expectedOutput: []logrus.Entry{
    76  				{Level: logrus.ErrorLevel, Message: "The bootstrap machine did not execute the bootkube.service systemd unit"},
    77  			},
    78  		},
    79  		{
    80  			name: "release-image and API Server URL successful",
    81  			files: map[string]string{
    82  				"log-bundle/bootstrap/services/release-image.json": generateSuccessOutput("pull-release-image"),
    83  				"log-bundle/bootstrap/services/bootkube.json":      generateSuccessOutput("check-api-url"),
    84  			},
    85  		},
    86  		{
    87  			name: "release-image and API Server URL successful bootstrap-in-place",
    88  			files: map[string]string{
    89  				"log-bundle/log-bundle-bootstrap/bootstrap/services/release-image.json": generateSuccessOutput("pull-release-image"),
    90  				"log-bundle/bootstrap/services/bootkube.json":                           generateSuccessOutput("check-api-url"),
    91  			},
    92  		},
    93  		{
    94  			name: "only release-image failed",
    95  			files: map[string]string{
    96  				"log-bundle/bootstrap/services/release-image.json": generateFailureOutput("pull-release-image"),
    97  				"log-bundle/bootstrap/services/bootkube.json":      generateSuccessOutput("check-api-url"),
    98  			},
    99  			expectedOutput: failedReleaseImage(),
   100  		},
   101  		{
   102  			name: "API Server URL failed",
   103  			files: map[string]string{
   104  				"log-bundle/log-bundle-bootstrap/bootstrap/services/release-image.json": generateSuccessOutput("pull-release-image"),
   105  				"log-bundle/bootstrap/services/bootkube.json":                           generateFailureOutput("check-api-url"),
   106  			},
   107  			expectedOutput: failedURLChecks(),
   108  		},
   109  		{
   110  			name: "API-INT Server URL failed",
   111  			files: map[string]string{
   112  				"log-bundle/log-bundle-bootstrap/bootstrap/services/release-image.json": generateSuccessOutput("pull-release-image"),
   113  				"log-bundle/bootstrap/services/bootkube.json":                           generateFailureOutput("check-api-int-url"),
   114  			},
   115  			expectedOutput: failedURLChecks(),
   116  		},
   117  		{
   118  			name: "both release-image and API Server URLs failed",
   119  			files: map[string]string{
   120  				"log-bundle/log-bundle-bootstrap/bootstrap/services/release-image.json": generateFailureOutput("pull-release-image"),
   121  				"log-bundle/bootstrap/services/bootkube.json":                           generateFailureOutput("check-api-url"),
   122  			},
   123  			expectedOutput: failedReleaseImage(),
   124  		},
   125  		{
   126  			name: "empty release-image.json",
   127  			files: map[string]string{
   128  				"log-bundle/bootstrap/services/release-image.json": "",
   129  			},
   130  			expectedOutput: []logrus.Entry{
   131  				{Level: logrus.InfoLevel, Message: "Could not analyze the release-image.service: service entries file does not begin with a token: EOF"},
   132  				{Level: logrus.ErrorLevel, Message: "The bootstrap machine did not execute the release-image.service systemd unit"},
   133  			},
   134  		},
   135  		{
   136  			name: "empty bootkube.json",
   137  			files: map[string]string{
   138  				"log-bundle/bootstrap/services/release-image.json": generateSuccessOutput("pull-release-image"),
   139  				"log-bundle/bootstrap/services/bootkube.json":      "",
   140  			},
   141  			expectedOutput: []logrus.Entry{
   142  				{Level: logrus.InfoLevel, Message: "Could not analyze the bootkube.service: service entries file does not begin with a token: EOF"},
   143  				{Level: logrus.ErrorLevel, Message: "The bootstrap machine did not execute the bootkube.service systemd unit"},
   144  			},
   145  		},
   146  		{
   147  			name: "malformed release-image.json",
   148  			files: map[string]string{
   149  				"log-bundle/bootstrap/services/release-image.json": "{}",
   150  			},
   151  			expectedOutput: []logrus.Entry{
   152  				{Level: logrus.InfoLevel, Message: "Could not analyze the release-image.service: service entries file does not begin with an array"},
   153  				{Level: logrus.ErrorLevel, Message: "The bootstrap machine did not execute the release-image.service systemd unit"},
   154  			},
   155  		},
   156  		{
   157  			name: "malformed bootkube.json",
   158  			files: map[string]string{
   159  				"log-bundle/bootstrap/services/release-image.json": generateSuccessOutput("pull-release-image"),
   160  				"log-bundle/bootstrap/services/bootkube.json":      "{}",
   161  			},
   162  			expectedOutput: []logrus.Entry{
   163  				{Level: logrus.InfoLevel, Message: "Could not analyze the bootkube.service: service entries file does not begin with an array"},
   164  				{Level: logrus.ErrorLevel, Message: "The bootstrap machine did not execute the bootkube.service systemd unit"},
   165  			},
   166  		},
   167  	}
   168  	for _, tc := range cases {
   169  		t.Run(tc.name, func(t *testing.T) {
   170  			var gatherBuilder bytes.Buffer
   171  			gzipWriter := gzip.NewWriter(&gatherBuilder)
   172  			defer gzipWriter.Close()
   173  			tarWriter := tar.NewWriter(gzipWriter)
   174  			defer tarWriter.Close()
   175  			for filename, contents := range tc.files {
   176  				contentsAsBytes := []byte(contents)
   177  				if err := tarWriter.WriteHeader(&tar.Header{
   178  					Typeflag: tar.TypeReg,
   179  					Name:     filename,
   180  					Size:     int64(len(contentsAsBytes)),
   181  				}); err != nil {
   182  					t.Fatal(err)
   183  				}
   184  				if _, err := tarWriter.Write(contentsAsBytes); err != nil {
   185  					t.Fatal(err)
   186  				}
   187  			}
   188  			gzipWriter.Close()
   189  			hook := test.NewLocal(logrus.StandardLogger())
   190  			defer hook.Reset()
   191  			err := analyzeGatherBundle(&gatherBuilder)
   192  			assert.NoError(t, err, "unexpected error from analysis")
   193  			for i, e := range hook.Entries {
   194  				hook.Entries[i] = logrus.Entry{
   195  					Level:   e.Level,
   196  					Message: e.Message,
   197  				}
   198  			}
   199  			assert.Equal(t, tc.expectedOutput, hook.Entries)
   200  		})
   201  	}
   202  }