github.com/google/go-github/v74@v74.0.0/github/enterprise_manage_ghes_maintenance_test.go (about)

     1  // Copyright 2025 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"encoding/json"
    11  	"fmt"
    12  	"net/http"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/google/go-cmp/cmp"
    17  )
    18  
    19  func TestEnterpriseService_GetMaintenanceStatus(t *testing.T) {
    20  	t.Parallel()
    21  	client, mux, _ := setup(t)
    22  
    23  	mux.HandleFunc("/manage/v1/maintenance", func(w http.ResponseWriter, r *http.Request) {
    24  		testMethod(t, r, "GET")
    25  		testFormValues(t, r, values{
    26  			"uuid":          "1234-1234",
    27  			"cluster_roles": "primary",
    28  		})
    29  		fmt.Fprint(w, `[{
    30  			"hostname": "primary",
    31  			"uuid": "1b6cf518-f97c-11ed-8544-061d81f7eedb",
    32  			"status": "scheduled",
    33  			"scheduled_time": "2018-01-01T00:00:00+00:00",
    34  			"connection_services": [
    35  			{
    36  				"name": "git operations",
    37  				"number": 15
    38  			}
    39  			],
    40  			"can_unset_maintenance": true,
    41  			"ip_exception_list": [
    42  			"1.1.1.1"
    43  			],
    44  			"maintenance_mode_message": "Scheduled maintenance for upgrading."
    45  		}]`)
    46  	})
    47  
    48  	opt := &NodeQueryOptions{
    49  		UUID: Ptr("1234-1234"), ClusterRoles: Ptr("primary"),
    50  	}
    51  	ctx := context.Background()
    52  	maintenanceStatus, _, err := client.Enterprise.GetMaintenanceStatus(ctx, opt)
    53  	if err != nil {
    54  		t.Errorf("Enterprise.GetMaintenanceStatus returned error: %v", err)
    55  	}
    56  
    57  	want := []*MaintenanceStatus{{
    58  		Hostname:      Ptr("primary"),
    59  		UUID:          Ptr("1b6cf518-f97c-11ed-8544-061d81f7eedb"),
    60  		Status:        Ptr("scheduled"),
    61  		ScheduledTime: &Timestamp{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)},
    62  		ConnectionServices: []*ConnectionServiceItem{{
    63  			Name:   Ptr("git operations"),
    64  			Number: Ptr(15),
    65  		}},
    66  		CanUnsetMaintenance:    Ptr(true),
    67  		IPExceptionList:        []string{"1.1.1.1"},
    68  		MaintenanceModeMessage: Ptr("Scheduled maintenance for upgrading."),
    69  	}}
    70  	if !cmp.Equal(maintenanceStatus, want) {
    71  		t.Errorf("Enterprise.GetMaintenanceStatus returned %+v, want %+v", maintenanceStatus, want)
    72  	}
    73  
    74  	const methodName = "GetMaintenanceStatus"
    75  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    76  		got, resp, err := client.Enterprise.GetMaintenanceStatus(ctx, opt)
    77  		if got != nil {
    78  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    79  		}
    80  		return resp, err
    81  	})
    82  }
    83  
    84  func TestEnterpriseService_CreateMaintenance(t *testing.T) {
    85  	t.Parallel()
    86  	client, mux, _ := setup(t)
    87  
    88  	input := &MaintenanceOptions{
    89  		Enabled: true,
    90  		UUID:    Ptr("1234-1234"),
    91  		When:    Ptr("now"),
    92  		IPExceptionList: []string{
    93  			"1.1.1.1",
    94  		},
    95  		MaintenanceModeMessage: Ptr("Scheduled maintenance for upgrading."),
    96  	}
    97  
    98  	mux.HandleFunc("/manage/v1/maintenance", func(w http.ResponseWriter, r *http.Request) {
    99  		v := new(MaintenanceOptions)
   100  		assertNilError(t, json.NewDecoder(r.Body).Decode(v))
   101  
   102  		testMethod(t, r, "POST")
   103  		if !cmp.Equal(v, input) {
   104  			t.Errorf("Request body = %+v, want %+v", v, input)
   105  		}
   106  
   107  		fmt.Fprint(w, `[ { "hostname": "primary", "uuid": "1b6cf518-f97c-11ed-8544-061d81f7eedb", "message": "Scheduled maintenance for upgrading." } ]`)
   108  	})
   109  
   110  	ctx := context.Background()
   111  	maintenanceStatus, _, err := client.Enterprise.CreateMaintenance(ctx, true, input)
   112  	if err != nil {
   113  		t.Errorf("Enterprise.CreateMaintenance returned error: %v", err)
   114  	}
   115  
   116  	want := []*MaintenanceOperationStatus{{Hostname: Ptr("primary"), UUID: Ptr("1b6cf518-f97c-11ed-8544-061d81f7eedb"), Message: Ptr("Scheduled maintenance for upgrading.")}}
   117  	if diff := cmp.Diff(want, maintenanceStatus); diff != "" {
   118  		t.Errorf("diff mismatch (-want +got):\n%v", diff)
   119  	}
   120  
   121  	const methodName = "CreateMaintenance"
   122  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   123  		got, resp, err := client.Enterprise.CreateMaintenance(ctx, true, input)
   124  		if got != nil {
   125  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   126  		}
   127  		return resp, err
   128  	})
   129  }