Best Gauge code snippet using logger.Errorf
openstack.go
Source:openstack.go
...160 logger.Debug("Deleting openstack servers")161 defer logger.Debugf("Exiting deleting openstack servers")162 conn, err := clientconfig.NewServiceClient("compute", opts)163 if err != nil {164 logger.Errorf("%v", err)165 return false, nil166 }167 listOpts := servers.ListOpts{168 // FIXME(shardy) when gophercloud supports tags we should169 // filter by tag here170 // https://github.com/gophercloud/gophercloud/pull/1115171 // and Nova doesn't seem to support filter by Metadata, so for172 // now we do client side filtering below based on the173 // Metadata key (which matches the server properties).174 }175 allPages, err := servers.List(conn, listOpts).AllPages()176 if err != nil {177 logger.Errorf("%v", err)178 return false, nil179 }180 allServers, err := servers.ExtractServers(allPages)181 if err != nil {182 logger.Errorf("%v", err)183 return false, nil184 }185 serverObjects := []ObjectWithTags{}186 for _, server := range allServers {187 serverObjects = append(188 serverObjects, ObjectWithTags{189 ID: server.ID,190 Tags: server.Metadata})191 }192 filteredServers := filterObjects(serverObjects, filter)193 for _, server := range filteredServers {194 logger.Debugf("Deleting Server %q", server.ID)195 err = servers.Delete(conn, server.ID).ExtractErr()196 if err != nil {197 // Ignore the error if the server cannot be found and return with an appropriate message if it's another type of error198 if _, ok := err.(gophercloud.ErrDefault404); !ok {199 logger.Errorf("Deleting server %q failed: %v", server.ID, err)200 return false, nil201 }202 logger.Debugf("Cannot find server %q. It's probably already been deleted.", server.ID)203 }204 }205 return len(filteredServers) == 0, nil206}207func deletePorts(opts *clientconfig.ClientOpts, filter Filter, logger logrus.FieldLogger) (bool, error) {208 logger.Debug("Deleting openstack ports")209 defer logger.Debugf("Exiting deleting openstack ports")210 conn, err := clientconfig.NewServiceClient("network", opts)211 if err != nil {212 logger.Errorf("%v", err)213 return false, nil214 }215 tags := filterTags(filter)216 listOpts := ports.ListOpts{217 TagsAny: strings.Join(tags, ","),218 }219 allPages, err := ports.List(conn, listOpts).AllPages()220 if err != nil {221 logger.Errorf("%v", err)222 return false, nil223 }224 allPorts, err := ports.ExtractPorts(allPages)225 if err != nil {226 logger.Errorf("%v", err)227 return false, nil228 }229 for _, port := range allPorts {230 listOpts := floatingips.ListOpts{231 PortID: port.ID,232 }233 allPages, err := floatingips.List(conn, listOpts).AllPages()234 if err != nil {235 logger.Errorf("%v", err)236 return false, nil237 }238 allFIPs, err := floatingips.ExtractFloatingIPs(allPages)239 if err != nil {240 logger.Errorf("%v", err)241 return false, nil242 }243 for _, fip := range allFIPs {244 logger.Debugf("Deleting Floating IP %q", fip.ID)245 err = floatingips.Delete(conn, fip.ID).ExtractErr()246 if err != nil {247 // Ignore the error if the floating ip cannot be found and return with an appropriate message if it's another type of error248 if _, ok := err.(gophercloud.ErrDefault404); !ok {249 logger.Errorf("While deleting port %q, the deletion of the floating IP %q failed with error: %v", port.ID, fip.ID, err)250 return false, nil251 }252 logger.Debugf("Cannot find floating ip %q. It's probably already been deleted.", fip.ID)253 }254 }255 logger.Debugf("Deleting Port %q", port.ID)256 err = ports.Delete(conn, port.ID).ExtractErr()257 if err != nil {258 // This can fail when port is still in use so return/retry259 logger.Debugf("Deleting Port %q failed with error: %v", port.ID, err)260 return false, nil261 }262 }263 return len(allPorts) == 0, nil264}265func deleteSecurityGroups(opts *clientconfig.ClientOpts, filter Filter, logger logrus.FieldLogger) (bool, error) {266 logger.Debug("Deleting openstack security-groups")267 defer logger.Debugf("Exiting deleting openstack security-groups")268 conn, err := clientconfig.NewServiceClient("network", opts)269 if err != nil {270 logger.Errorf("%v", err)271 return false, nil272 }273 tags := filterTags(filter)274 listOpts := sg.ListOpts{275 TagsAny: strings.Join(tags, ","),276 }277 allPages, err := sg.List(conn, listOpts).AllPages()278 if err != nil {279 logger.Errorf("%v", err)280 return false, nil281 }282 allGroups, err := sg.ExtractGroups(allPages)283 if err != nil {284 logger.Errorf("%v", err)285 return false, nil286 }287 for _, group := range allGroups {288 logger.Debugf("Deleting Security Group: %q", group.ID)289 err = sg.Delete(conn, group.ID).ExtractErr()290 if err != nil {291 // Ignore the error if the security group cannot be found292 if _, ok := err.(gophercloud.ErrDefault404); !ok {293 // This can fail when sg is still in use by servers294 logger.Debugf("Deleting Security Group %q failed with error: %v", group.ID, err)295 return false, nil296 }297 logger.Debugf("Cannot find security group %q. It's probably already been deleted.", group.ID)298 }299 }300 return len(allGroups) == 0, nil301}302func deleteRouters(opts *clientconfig.ClientOpts, filter Filter, logger logrus.FieldLogger) (bool, error) {303 logger.Debug("Deleting openstack routers")304 defer logger.Debugf("Exiting deleting openstack routers")305 conn, err := clientconfig.NewServiceClient("network", opts)306 if err != nil {307 logger.Errorf("%v", err)308 return false, nil309 }310 tags := filterTags(filter)311 listOpts := routers.ListOpts{312 TagsAny: strings.Join(tags, ","),313 }314 allPages, err := routers.List(conn, listOpts).AllPages()315 if err != nil {316 logger.Errorf("%v", err)317 return false, nil318 }319 allRouters, err := routers.ExtractRouters(allPages)320 if err != nil {321 logger.Errorf("%v", err)322 return false, nil323 }324 for _, router := range allRouters {325 // If a user provisioned floating ip was used, it needs to be dissociated326 // Any floating Ip's associated with routers that are going to be deleted will be dissociated327 fipOpts := floatingips.ListOpts{328 RouterID: router.ID,329 }330 fipPages, err := floatingips.List(conn, fipOpts).AllPages()331 if err != nil {332 logger.Errorf("%v", err)333 return false, nil334 }335 allFIPs, err := floatingips.ExtractFloatingIPs(fipPages)336 if err != nil {337 logger.Errorf("%v", err)338 return false, nil339 }340 for _, fip := range allFIPs {341 _, err := floatingips.Update(conn, fip.ID, floatingips.UpdateOpts{}).Extract()342 if err != nil {343 // Ignore the error if the resource cannot be found and return with an appropriate message if it's another type of error344 if _, ok := err.(gophercloud.ErrDefault404); !ok {345 logger.Errorf("Updating floating IP %q for Router %q failed: %v", fip.ID, router.ID, err)346 return false, nil347 }348 logger.Debugf("Cannot find floating ip %q. It's probably already been deleted.", fip.ID)349 }350 }351 // Clean Gateway interface352 updateOpts := routers.UpdateOpts{353 GatewayInfo: &routers.GatewayInfo{},354 }355 _, err = routers.Update(conn, router.ID, updateOpts).Extract()356 if err != nil {357 logger.Errorf("%v", err)358 }359 // Get router interface ports360 portListOpts := ports.ListOpts{361 DeviceID: router.ID,362 }363 allPagesPort, err := ports.List(conn, portListOpts).AllPages()364 if err != nil {365 logger.Errorf("%v", err)366 return false, nil367 }368 allPorts, err := ports.ExtractPorts(allPagesPort)369 if err != nil {370 logger.Errorf("%v", err)371 return false, nil372 }373 // map to keep track of whethere interface for subnet was already removed374 removedSubnets := make(map[string]bool)375 for _, port := range allPorts {376 for _, IP := range port.FixedIPs {377 if !removedSubnets[IP.SubnetID] {378 removeOpts := routers.RemoveInterfaceOpts{379 SubnetID: IP.SubnetID,380 }381 logger.Debugf("Removing Subnet %q from Router %q", IP.SubnetID, router.ID)382 _, err = routers.RemoveInterface(conn, router.ID, removeOpts).Extract()383 if err != nil {384 if _, ok := err.(gophercloud.ErrDefault404); !ok {385 // This can fail when subnet is still in use386 logger.Debugf("Removing Subnet %q from Router %q failed: %v", IP.SubnetID, router.ID, err)387 return false, nil388 }389 logger.Debugf("Cannot find subnet %q. It's probably already been removed from router %q.", IP.SubnetID, router.ID)390 }391 removedSubnets[IP.SubnetID] = true392 }393 }394 }395 logger.Debugf("Deleting Router %q", router.ID)396 err = routers.Delete(conn, router.ID).ExtractErr()397 if err != nil {398 // Ignore the error if the router cannot be found and return with an appropriate message if it's another type of error399 if _, ok := err.(gophercloud.ErrDefault404); !ok {400 logger.Errorf("Deleting router %q failed: %v", router.ID, err)401 return false, nil402 }403 logger.Debugf("Cannot find router %q. It's probably already been deleted.", router.ID)404 }405 }406 return len(allRouters) == 0, nil407}408func deleteSubnets(opts *clientconfig.ClientOpts, filter Filter, logger logrus.FieldLogger) (bool, error) {409 logger.Debug("Deleting openstack subnets")410 defer logger.Debugf("Exiting deleting openstack subnets")411 conn, err := clientconfig.NewServiceClient("network", opts)412 if err != nil {413 logger.Errorf("%v", err)414 return false, nil415 }416 tags := filterTags(filter)417 listOpts := subnets.ListOpts{418 TagsAny: strings.Join(tags, ","),419 }420 allPages, err := subnets.List(conn, listOpts).AllPages()421 if err != nil {422 logger.Errorf("%v", err)423 return false, nil424 }425 allSubnets, err := subnets.ExtractSubnets(allPages)426 if err != nil {427 logger.Errorf("%v", err)428 return false, nil429 }430 for _, subnet := range allSubnets {431 logger.Debugf("Deleting Subnet: %q", subnet.ID)432 err = subnets.Delete(conn, subnet.ID).ExtractErr()433 if err != nil {434 // Ignore the error if the subnet cannot be found435 if _, ok := err.(gophercloud.ErrDefault404); !ok {436 // This can fail when subnet is still in use437 logger.Debugf("Deleting Subnet %q failed: %v", subnet.ID, err)438 return false, nil439 }440 logger.Debugf("Cannot find subnet %q. It's probably already been deleted.", subnet.ID)441 }442 }443 return len(allSubnets) == 0, nil444}445func deleteNetworks(opts *clientconfig.ClientOpts, filter Filter, logger logrus.FieldLogger) (bool, error) {446 logger.Debug("Deleting openstack networks")447 defer logger.Debugf("Exiting deleting openstack networks")448 conn, err := clientconfig.NewServiceClient("network", opts)449 if err != nil {450 logger.Errorf("%v", err)451 return false, nil452 }453 tags := filterTags(filter)454 listOpts := networks.ListOpts{455 TagsAny: strings.Join(tags, ","),456 }457 allPages, err := networks.List(conn, listOpts).AllPages()458 if err != nil {459 logger.Errorf("%v", err)460 return false, nil461 }462 allNetworks, err := networks.ExtractNetworks(allPages)463 if err != nil {464 logger.Errorf("%v", err)465 return false, nil466 }467 for _, network := range allNetworks {468 logger.Debugf("Deleting network: %q", network.ID)469 err = networks.Delete(conn, network.ID).ExtractErr()470 if err != nil {471 // Ignore the error if the network cannot be found472 if _, ok := err.(gophercloud.ErrDefault404); !ok {473 // This can fail when network is still in use474 logger.Debugf("Deleting Network %q failed: %v", network.ID, err)475 return false, nil476 }477 logger.Debugf("Cannot find network %q. It's probably already been deleted.", network.ID)478 }479 }480 return len(allNetworks) == 0, nil481}482func deleteContainers(opts *clientconfig.ClientOpts, filter Filter, logger logrus.FieldLogger) (bool, error) {483 logger.Debug("Deleting openstack containers")484 defer logger.Debugf("Exiting deleting openstack containers")485 conn, err := clientconfig.NewServiceClient("object-store", opts)486 if err != nil {487 logger.Errorf("%v", err)488 return false, nil489 }490 listOpts := containers.ListOpts{Full: false}491 allPages, err := containers.List(conn, listOpts).AllPages()492 if err != nil {493 logger.Errorf("%v", err)494 return false, nil495 }496 allContainers, err := containers.ExtractNames(allPages)497 if err != nil {498 logger.Errorf("%v", err)499 return false, nil500 }501 for _, container := range allContainers {502 metadata, err := containers.Get(conn, container, nil).ExtractMetadata()503 if err != nil {504 logger.Errorf("%v", err)505 return false, nil506 }507 for key, val := range filter {508 // Swift mangles the case so openshiftClusterID becomes509 // Openshiftclusterid in the X-Container-Meta- HEAD output510 titlekey := strings.Title(strings.ToLower(key))511 if metadata[titlekey] == val {512 listOpts := objects.ListOpts{Full: false}513 allPages, err := objects.List(conn, container, listOpts).AllPages()514 if err != nil {515 logger.Errorf("%v", err)516 return false, nil517 }518 allObjects, err := objects.ExtractNames(allPages)519 if err != nil {520 logger.Errorf("%v", err)521 return false, nil522 }523 for _, object := range allObjects {524 logger.Debugf("Deleting object %q", object)525 _, err = objects.Delete(conn, container, object, nil).Extract()526 if err != nil {527 // Ignore the error if the object cannot be found and return with an appropriate message if it's another type of error528 if _, ok := err.(gophercloud.ErrDefault404); !ok {529 logger.Errorf("Removing object %q from container %q failed: %v", object, container, err)530 return false, nil531 }532 logger.Debugf("Cannot find object %q in container %q. It's probably already been deleted.", object, container)533 }534 }535 logger.Debugf("Deleting container %q", container)536 _, err = containers.Delete(conn, container).Extract()537 if err != nil {538 // Ignore the error if the container cannot be found and return with an appropriate message if it's another type of error539 if _, ok := err.(gophercloud.ErrDefault404); !ok {540 logger.Errorf("Deleting container %q failed: %v", container, err)541 return false, nil542 }543 logger.Debugf("Cannot find container %q. It's probably already been deleted.", container)544 }545 // If a metadata key matched, we're done so break from the loop546 break547 }548 }549 }550 return true, nil551}552func deleteTrunks(opts *clientconfig.ClientOpts, filter Filter, logger logrus.FieldLogger) (bool, error) {553 logger.Debug("Deleting openstack trunks")554 defer logger.Debugf("Exiting deleting openstack trunks")555 conn, err := clientconfig.NewServiceClient("network", opts)556 if err != nil {557 logger.Errorf("%v", err)558 return false, nil559 }560 tags := filterTags(filter)561 listOpts := trunks.ListOpts{562 TagsAny: strings.Join(tags, ","),563 }564 allPages, err := trunks.List(conn, listOpts).AllPages()565 if err != nil {566 logger.Errorf("%v", err)567 return false, nil568 }569 allTrunks, err := trunks.ExtractTrunks(allPages)570 if err != nil {571 logger.Errorf("%v", err)572 return false, nil573 }574 for _, trunk := range allTrunks {575 logger.Debugf("Deleting Trunk %q", trunk.ID)576 err = trunks.Delete(conn, trunk.ID).ExtractErr()577 if err != nil {578 // Ignore the error if the trunk cannot be found579 if _, ok := err.(gophercloud.ErrDefault404); !ok {580 // This can fail when the trunk is still in use so return/retry581 logger.Debugf("Deleting Trunk %q failed: %v", trunk.ID, err)582 return false, nil583 }584 logger.Debugf("Cannot find trunk %q. It's probably already been deleted.", trunk.ID)585 }586 }587 return len(allTrunks) == 0, nil588}589func deleteLoadBalancers(opts *clientconfig.ClientOpts, filter Filter, logger logrus.FieldLogger) (bool, error) {590 logger.Debug("Deleting openstack load balancers")591 defer logger.Debugf("Exiting deleting openstack load balancers")592 conn, err := clientconfig.NewServiceClient("load-balancer", opts)593 if err != nil {594 // Ignore the error if Octavia is not available for the cloud595 if _, ok := err.(*gophercloud.ErrEndpointNotFound); ok {596 logger.Debug("Skip load balancer deletion because Octavia endpoint is not found")597 return true, nil598 }599 logger.Errorf("%v", err)600 return false, nil601 }602 newallPages, err := apiversions.List(conn).AllPages()603 if err != nil {604 logger.Errorf("Unable to list api versions: %v", err)605 return false, nil606 }607 allAPIVersions, err := apiversions.ExtractAPIVersions(newallPages)608 if err != nil {609 logger.Errorf("Unable to extract api versions: %v", err)610 return false, nil611 }612 var octaviaTagSupport bool613 octaviaTagSupport = false614 for _, apiVersion := range allAPIVersions {615 if apiVersion.ID >= minOctaviaVersionWithTagSupport {616 octaviaTagSupport = true617 }618 }619 tags := filterTags(filter)620 var allLoadBalancers []loadbalancers.LoadBalancer621 if octaviaTagSupport {622 listOpts := loadbalancers.ListOpts{623 TagsAny: tags,624 }625 allPages, err := loadbalancers.List(conn, listOpts).AllPages()626 if err != nil {627 logger.Errorf("%v", err)628 return false, nil629 }630 allLoadBalancers, err = loadbalancers.ExtractLoadBalancers(allPages)631 if err != nil {632 logger.Errorf("%v", err)633 return false, nil634 }635 }636 listOpts := loadbalancers.ListOpts{637 Description: strings.Join(tags, ","),638 }639 allPages, err := loadbalancers.List(conn, listOpts).AllPages()640 if err != nil {641 logger.Errorf("%v", err)642 return false, nil643 }644 allLoadBalancersWithTaggedDescription, err := loadbalancers.ExtractLoadBalancers(allPages)645 if err != nil {646 logger.Errorf("%v", err)647 return false, nil648 }649 allLoadBalancers = append(allLoadBalancers, allLoadBalancersWithTaggedDescription...)650 deleteOpts := loadbalancers.DeleteOpts{651 Cascade: true,652 }653 for _, loadbalancer := range allLoadBalancers {654 logger.Debugf("Deleting LoadBalancer %q", loadbalancer.ID)655 err = loadbalancers.Delete(conn, loadbalancer.ID, deleteOpts).ExtractErr()656 if err != nil {657 // Ignore the error if the load balancer cannot be found658 if _, ok := err.(gophercloud.ErrDefault404); !ok {659 // This can fail when the load balancer is still in use so return/retry660 logger.Debugf("Deleting load balancer %q failed: %v", loadbalancer.ID, err)661 return false, nil662 }663 logger.Debugf("Cannot find load balancer %q. It's probably already been deleted.", loadbalancer.ID)664 }665 }666 return len(allLoadBalancers) == 0, nil667}668func deleteSubnetPools(opts *clientconfig.ClientOpts, filter Filter, logger logrus.FieldLogger) (bool, error) {669 logger.Debug("Deleting openstack subnet-pools")670 defer logger.Debugf("Exiting deleting openstack subnet-pools")671 conn, err := clientconfig.NewServiceClient("network", opts)672 if err != nil {673 logger.Errorf("%v", err)674 return false, nil675 }676 tags := filterTags(filter)677 listOpts := subnetpools.ListOpts{678 TagsAny: strings.Join(tags, ","),679 }680 allPages, err := subnetpools.List(conn, listOpts).AllPages()681 if err != nil {682 logger.Errorf("%v", err)683 return false, nil684 }685 allSubnetPools, err := subnetpools.ExtractSubnetPools(allPages)686 if err != nil {687 logger.Errorf("%v", err)688 return false, nil689 }690 for _, subnetPool := range allSubnetPools {691 logger.Debugf("Deleting Subnet Pool %q", subnetPool.ID)692 err = subnetpools.Delete(conn, subnetPool.ID).ExtractErr()693 if err != nil {694 // Ignore the error if the subnet pool cannot be found695 if _, ok := err.(gophercloud.ErrDefault404); !ok {696 logger.Debugf("Deleting subnet pool %q failed: %v", subnetPool.ID, err)697 return false, nil698 }699 logger.Debugf("Cannot find subnet pool %q. It's probably already been deleted.", subnetPool.ID)700 }701 }702 return len(allSubnetPools) == 0, nil703}704func deleteVolumes(opts *clientconfig.ClientOpts, filter Filter, logger logrus.FieldLogger) (bool, error) {705 logger.Debug("Deleting OpenStack volumes")706 defer logger.Debugf("Exiting deleting OpenStack volumes")707 // We need to delete all volumes that have names with the cluster ID as a prefix708 var clusterID string709 for k, v := range filter {710 if strings.ToLower(k) == "openshiftclusterid" {711 clusterID = v712 break713 }714 }715 conn, err := clientconfig.NewServiceClient("volume", opts)716 if err != nil {717 logger.Errorf("%v", err)718 return false, nil719 }720 listOpts := volumes.ListOpts{}721 allPages, err := volumes.List(conn, listOpts).AllPages()722 if err != nil {723 logger.Errorf("%v", err)724 return false, nil725 }726 allVolumes, err := volumes.ExtractVolumes(allPages)727 if err != nil {728 logger.Errorf("%v", err)729 return false, nil730 }731 volumeIDs := []string{}732 for _, volume := range allVolumes {733 if strings.HasPrefix(volume.Name, clusterID) {734 volumeIDs = append(volumeIDs, volume.ID)735 }736 }737 deleteOpts := volumes.DeleteOpts{738 Cascade: false,739 }740 for _, volumeID := range volumeIDs {741 logger.Debugf("Deleting volume %q", volumeID)742 err = volumes.Delete(conn, volumeID, deleteOpts).ExtractErr()743 if err != nil {744 // Ignore the error if the server cannot be found745 if _, ok := err.(gophercloud.ErrDefault404); !ok {746 logger.Debugf("Deleting volume %q failed: %v", volumeID, err)747 return false, nil748 }749 logger.Debugf("Cannot find volume %q. It's probably already been deleted.", volumeID)750 }751 }752 return true, nil753}754func deleteFloatingIPs(opts *clientconfig.ClientOpts, filter Filter, logger logrus.FieldLogger) (bool, error) {755 logger.Debug("Deleting openstack floating ips")756 defer logger.Debugf("Exiting deleting openstack floating ips")757 conn, err := clientconfig.NewServiceClient("network", opts)758 if err != nil {759 logger.Errorf("%v", err)760 return false, nil761 }762 tags := filterTags(filter)763 listOpts := floatingips.ListOpts{764 TagsAny: strings.Join(tags, ","),765 }766 allPages, err := floatingips.List(conn, listOpts).AllPages()767 if err != nil {768 logger.Errorf("%v", err)769 return false, nil770 }771 allFloatingIPs, err := floatingips.ExtractFloatingIPs(allPages)772 if err != nil {773 logger.Errorf("%v", err)774 return false, nil775 }776 for _, floatingIP := range allFloatingIPs {777 logger.Debugf("Deleting Floating IP %q", floatingIP.ID)778 err = floatingips.Delete(conn, floatingIP.ID).ExtractErr()779 if err != nil {780 // Ignore the error if the floating ip cannot be found781 if _, ok := err.(gophercloud.ErrDefault404); !ok {782 logger.Debugf("Deleting floating ip %q failed: %v", floatingIP.ID, err)783 return false, nil784 }785 logger.Debugf("Cannot find floating ip %q. It's probably already been deleted.", floatingIP.ID)786 }787 }788 return len(allFloatingIPs) == 0, nil789}790func deleteImages(opts *clientconfig.ClientOpts, filter Filter, logger logrus.FieldLogger) (bool, error) {791 logger.Debug("Deleting openstack base image")792 defer logger.Debugf("Exiting deleting openstack base image")793 conn, err := clientconfig.NewServiceClient("image", opts)794 if err != nil {795 logger.Errorf("%v", err)796 return false, nil797 }798 listOpts := images.ListOpts{799 Tags: filterTags(filter),800 }801 allPages, err := images.List(conn, listOpts).AllPages()802 if err != nil {803 logger.Errorf("%v", err)804 return false, nil805 }806 allImages, err := images.ExtractImages(allPages)807 if err != nil {808 logger.Errorf("%v", err)809 return false, nil810 }811 for _, image := range allImages {812 logger.Debugf("Deleting image: %+v", image.ID)813 err := images.Delete(conn, image.ID).ExtractErr()814 if err != nil {815 // This can fail if the image is still in use by other VMs816 logger.Debugf("Deleting Image failed: %v", err)817 return false, nil818 }819 }820 return true, nil821}...
mock_test.go
Source:mock_test.go
...6func TestMockDebugNoArgs(t *testing.T) {7 logger := &Mock{}8 logger.Debug()9 if logger.DebugMessage != "" {10 t.Errorf("expected no debug message got %q", logger.DebugMessage)11 }12}13func TestMockDebug(t *testing.T) {14 tests := []struct {15 name string16 args []interface{}17 }{18 {"empty string", []interface{}{""}},19 {"one string", []interface{}{"str"}},20 {"two strings", []interface{}{"str1", "str2"}},21 {"two strings and a bool", []interface{}{"str1", "str2", false}},22 {"string, bool and float64", []interface{}{"str", false, 0.03}},23 }24 for _, tt := range tests {25 t.Run(tt.name, func(t *testing.T) {26 logger := &Mock{}27 logger.Debug(tt.args...)28 sprint := fmt.Sprint(tt.args...)29 if logger.DebugMessage != sprint {30 t.Errorf("expected %q got %q", sprint, logger.DebugMessage)31 }32 if logger.InfoMessage != "" {33 t.Errorf("expected no info message got %q", logger.InfoMessage)34 }35 if logger.ErrorMessage != "" {36 t.Errorf("expected no error message got %q", logger.ErrorMessage)37 }38 if logger.FatalMessage != "" {39 t.Errorf("expected no fatal message got %q", logger.FatalMessage)40 }41 })42 }43}44func TestMockDebugf(t *testing.T) {45 tests := []struct {46 name string47 format string48 args []interface{}49 }{50 {"one string", "%s", []interface{}{"str"}},51 {"quoted string", "%q", []interface{}{"str"}},52 {"two strings joined", "%s%s", []interface{}{"str1", "str2"}},53 {"two strings spaced", "%s %s", []interface{}{"str1", "str2"}},54 {"two strings and a bool", "%s %s %t", []interface{}{"str1", "str2", false}},55 {"string, bool and float64", "%s %t %f", []interface{}{"str", false, 0.03}},56 }57 for _, tt := range tests {58 t.Run(tt.name, func(t *testing.T) {59 logger := &Mock{}60 logger.Debugf(tt.format, tt.args...)61 sprintf := fmt.Sprintf(tt.format, tt.args...)62 if logger.DebugMessage != sprintf {63 t.Errorf("expected %q got %q", sprintf, logger.DebugMessage)64 }65 if logger.InfoMessage != "" {66 t.Errorf("expected no info message got %q", logger.InfoMessage)67 }68 if logger.ErrorMessage != "" {69 t.Errorf("expected no error message got %q", logger.ErrorMessage)70 }71 if logger.FatalMessage != "" {72 t.Errorf("expected no fatal message got %q", logger.FatalMessage)73 }74 })75 }76}77func TestMockInfoNoArgs(t *testing.T) {78 logger := &Mock{}79 logger.Info()80 if logger.InfoMessage != "" {81 t.Errorf("expected no info message got %q", logger.InfoMessage)82 }83}84func TestMockInfo(t *testing.T) {85 tests := []struct {86 name string87 args []interface{}88 }{89 {"empty string", []interface{}{""}},90 {"one string", []interface{}{"str"}},91 {"two strings", []interface{}{"str1", "str2"}},92 {"two strings and a bool", []interface{}{"str1", "str2", false}},93 {"string, bool and float64", []interface{}{"str", false, 0.03}},94 }95 for _, tt := range tests {96 t.Run(tt.name, func(t *testing.T) {97 logger := &Mock{}98 logger.Info(tt.args...)99 sprint := fmt.Sprint(tt.args...)100 if logger.InfoMessage != sprint {101 t.Errorf("expected %q got %q", sprint, logger.InfoMessage)102 }103 if logger.DebugMessage != "" {104 t.Errorf("expected no debug message got %q", logger.DebugMessage)105 }106 if logger.ErrorMessage != "" {107 t.Errorf("expected no error message got %q", logger.ErrorMessage)108 }109 if logger.FatalMessage != "" {110 t.Errorf("expected no fatal message got %q", logger.FatalMessage)111 }112 })113 }114}115func TestMockInfof(t *testing.T) {116 tests := []struct {117 name string118 format string119 args []interface{}120 }{121 {"one string", "%s", []interface{}{"str"}},122 {"quoted string", "%q", []interface{}{"str"}},123 {"two strings joined", "%s%s", []interface{}{"str1", "str2"}},124 {"two strings spaced", "%s %s", []interface{}{"str1", "str2"}},125 {"two strings and a bool", "%s %s %t", []interface{}{"str1", "str2", false}},126 {"string, bool and float64", "%s %t %f", []interface{}{"str", false, 0.03}},127 }128 for _, tt := range tests {129 t.Run(tt.name, func(t *testing.T) {130 logger := &Mock{}131 logger.Infof(tt.format, tt.args...)132 sprintf := fmt.Sprintf(tt.format, tt.args...)133 if logger.InfoMessage != sprintf {134 t.Errorf("expected %q got %q", sprintf, logger.InfoMessage)135 }136 if logger.DebugMessage != "" {137 t.Errorf("expected no debug message got %q", logger.DebugMessage)138 }139 if logger.ErrorMessage != "" {140 t.Errorf("expected no error message got %q", logger.ErrorMessage)141 }142 if logger.FatalMessage != "" {143 t.Errorf("expected no fatal message got %q", logger.FatalMessage)144 }145 })146 }147}148func TestMockErrorNoArgs(t *testing.T) {149 logger := &Mock{}150 logger.Error()151 if logger.ErrorMessage != "" {152 t.Errorf("expected no error message got %q", logger.ErrorMessage)153 }154}155func TestMockError(t *testing.T) {156 tests := []struct {157 name string158 args []interface{}159 }{160 {"empty string", []interface{}{""}},161 {"one string", []interface{}{"str"}},162 {"two strings", []interface{}{"str1", "str2"}},163 {"two strings and a bool", []interface{}{"str1", "str2", false}},164 {"string, bool and float64", []interface{}{"str", false, 0.03}},165 }166 for _, tt := range tests {167 t.Run(tt.name, func(t *testing.T) {168 logger := &Mock{}169 logger.Error(tt.args...)170 sprint := fmt.Sprint(tt.args...)171 if logger.ErrorMessage != sprint {172 t.Errorf("expected %q got %q", sprint, logger.ErrorMessage)173 }174 if logger.DebugMessage != "" {175 t.Errorf("expected no debug message got %q", logger.DebugMessage)176 }177 if logger.InfoMessage != "" {178 t.Errorf("expected no info message got %q", logger.InfoMessage)179 }180 if logger.FatalMessage != "" {181 t.Errorf("expected no fatal message got %q", logger.FatalMessage)182 }183 })184 }185}186func TestMockErrorf(t *testing.T) {187 tests := []struct {188 name string189 format string190 args []interface{}191 }{192 {"one string", "%s", []interface{}{"str"}},193 {"quoted string", "%q", []interface{}{"str"}},194 {"two strings joined", "%s%s", []interface{}{"str1", "str2"}},195 {"two strings spaced", "%s %s", []interface{}{"str1", "str2"}},196 {"two strings and a bool", "%s %s %t", []interface{}{"str1", "str2", false}},197 {"string, bool and float64", "%s %t %f", []interface{}{"str", false, 0.03}},198 }199 for _, tt := range tests {200 t.Run(tt.name, func(t *testing.T) {201 logger := &Mock{}202 logger.Errorf(tt.format, tt.args...)203 sprintf := fmt.Sprintf(tt.format, tt.args...)204 if logger.ErrorMessage != sprintf {205 t.Errorf("expected %q got %q", sprintf, logger.ErrorMessage)206 }207 if logger.DebugMessage != "" {208 t.Errorf("expected no debug message got %q", logger.DebugMessage)209 }210 if logger.InfoMessage != "" {211 t.Errorf("expected no info message got %q", logger.InfoMessage)212 }213 if logger.FatalMessage != "" {214 t.Errorf("expected no fatal message got %q", logger.FatalMessage)215 }216 })217 }218}219func TestMockFatalNoArgs(t *testing.T) {220 logger := &Mock{}221 logger.Fatal()222 if logger.FatalMessage != "" {223 t.Fatalf("expected no fatal message got %q", logger.FatalMessage)224 }225}226func TestMockFatal(t *testing.T) {227 tests := []struct {228 name string229 args []interface{}230 }{231 {"empty string", []interface{}{""}},232 {"one string", []interface{}{"str"}},233 {"two strings", []interface{}{"str1", "str2"}},234 {"two strings and a bool", []interface{}{"str1", "str2", false}},235 {"string, bool and float64", []interface{}{"str", false, 0.03}},236 }237 for _, tt := range tests {238 t.Run(tt.name, func(t *testing.T) {239 logger := &Mock{}240 logger.Fatal(tt.args...)241 sprint := fmt.Sprint(tt.args...)242 if logger.FatalMessage != sprint {243 t.Fatalf("expected %q got %q", sprint, logger.FatalMessage)244 }245 if logger.DebugMessage != "" {246 t.Errorf("expected no debug message got %q", logger.DebugMessage)247 }248 if logger.InfoMessage != "" {249 t.Errorf("expected no info message got %q", logger.InfoMessage)250 }251 if logger.ErrorMessage != "" {252 t.Errorf("expected no error message got %q", logger.ErrorMessage)253 }254 })255 }256}257func TestMockFatalf(t *testing.T) {258 tests := []struct {259 name string260 format string261 args []interface{}262 }{263 {"one string", "%s", []interface{}{"str"}},264 {"quoted string", "%q", []interface{}{"str"}},265 {"two strings joined", "%s%s", []interface{}{"str1", "str2"}},266 {"two strings spaced", "%s %s", []interface{}{"str1", "str2"}},267 {"two strings and a bool", "%s %s %t", []interface{}{"str1", "str2", false}},268 {"string, bool and float64", "%s %t %f", []interface{}{"str", false, 0.03}},269 }270 for _, tt := range tests {271 t.Run(tt.name, func(t *testing.T) {272 logger := &Mock{}273 logger.Fatalf(tt.format, tt.args...)274 sprintf := fmt.Sprintf(tt.format, tt.args...)275 if logger.FatalMessage != sprintf {276 t.Fatalf("expected %q got %q", sprintf, logger.FatalMessage)277 }278 if logger.DebugMessage != "" {279 t.Errorf("expected no debug message got %q", logger.DebugMessage)280 }281 if logger.InfoMessage != "" {282 t.Errorf("expected no info message got %q", logger.InfoMessage)283 }284 if logger.ErrorMessage != "" {285 t.Errorf("expected no error message got %q", logger.ErrorMessage)286 }287 })288 }289}...
Errorf
Using AI Code Generation
1import (2func main() {3 file, err := os.Create("test.txt")4 if err != nil {5 log.Fatalf("failed to create file: %s", err)6 }7 defer file.Close()8 log.SetOutput(file)9 log.Println("This is a test log entry")10}11import (12func main() {13 file, err := os.Create("test.txt")14 if err != nil {15 log.Fatalf("failed to create file: %s", err)16 }17 defer file.Close()18 log.SetOutput(file)19 log.Fatalln("This is a test log entry")20}21log.(*Logger).Panicln(0xc0000a6000, 0xc0000a4000, 0x1, 0x1)22log.Panicln(0xc0000a4000, 0x1, 0x1)23main.main()24import (25func main() {26 file, err := os.Create("test.txt")27 if err != nil {28 log.Fatalf("failed to create file: %s", err)29 }30 defer file.Close()31 log.SetOutput(file)32 log.Panicln("This is a test log entry")33}34log.(*Logger).Panicln(0
Errorf
Using AI Code Generation
1import (2func main() {3 f, err := os.Open("test.txt")4 if err != nil {5 log.Fatalf("Error opening file: %v", err)6 }7 defer f.Close()8}9import (10func main() {11 f, err := os.Open("test.txt")12 if err != nil {13 log.Fatalf("Error opening file: %v", err)14 }15 defer f.Close()16}17import (18func main() {19 f, err := os.Open("test.txt")20 if err != nil {21 log.Panicf("Error opening file: %v", err)22 }23 defer f.Close()24}25log.Panicf(0x4f3a7e, 0x15, 0xc00004bf50, 0x1, 0x1)26main.main()27import (28func main() {29 f, err := os.Open("test.txt")30 if err != nil {31 log.Panicln("Error opening file: ", err)32 }33 defer f.Close()34}
Errorf
Using AI Code Generation
1import (2func main() {3 f, err := os.Open("test.txt")4 if err != nil {5 log.Fatalf("Error opening file: %v", err)6 }7 defer f.Close()8}9import (10func main() {11 f, err := os.Open("test.txt")12 if err != nil {13 log.Fatalf("Error opening file: %v", err)14 }15 defer f.Close()16}17import (18func main() {19 f, err := os.Open("test.txt")20 if err != nil {21 log.Panicf("Error opening file: %v", err)22 }23 defer f.Close()24}25import (26func main() {27 f, err := os.Open("test.txt")28 if err != nil {29 log.Printf("Error opening file: %v", err)30 }31 defer f.Close()32}33import (34func main() {35 f, err := os.Open("test.txt")36 if err != nil {37 log.Println("Error opening file: %v", err)38 }39 defer f.Close()40}41import (42func main() {43 f, err := os.Open("test.txt")44 if err != nil {45 log.Print("Error opening file: %v", err)46 }47 defer f.Close()48}49import (50func main() {51 f, err := os.Open("test.txt")52 if err != nil {53 log.Fatal("Error opening file: %v", err)54 }55 defer f.Close()56}57import (58func main() {
Errorf
Using AI Code Generation
1import (2func main() {3 log.Fatalf("Error %s", "occured")4 log.Panicf("Error %s", "occured")5 log.Printf("Error %s", "occured")6}7log.Panicf(0x4e7c5b, 0xc, 0xc0000a3f58, 0x1, 0x1)8main.main()9log.Panicf(...)10main.main()
Errorf
Using AI Code Generation
1import (2func main() {3 file, err := os.Create("test.txt")4 if err != nil {5 log.Fatalf("failed creating file: %s", err)6 }7 defer file.Close()8 fmt.Println("file created")9 log.SetOutput(file)10 log.Println("This is a sample log message")11 log.Printf("This is a sample log message with a value %d", 10)12 log.Fatalf("This is a fatal log message")13 log.Panicf("This is a panic log message")14}
Errorf
Using AI Code Generation
1logger := log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)2logger.Printf("This is a message")3logger.Fatalf("This is a fatal message")4logger.Panicf("This is a panic message")5panic(0x4b3f60, 0xc82000a1e0)6main.main()7logger := log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)8logger.Print("This is a message")9logger.Fatal("This is a fatal message")10logger.Panic("This is a panic message")11logger := log.New(os.Stdout, "INFO: ", log.L
Errorf
Using AI Code Generation
1import (2func main() {3 log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)4 log.Printf("This is a log message")5 log.Fatalf("This is a fatal log message")6 fmt.Println("This is a normal message")7}
Errorf
Using AI Code Generation
1import (2func main() {3 log.Fatalf("Error: %s", "Fatal error")4}5log.Fatalln()6import (7func main() {8 log.Fatalln("Error:", "Fatal error")9}10log.Panic()11import (12func main() {13 log.Panic("Error:", "Panic error")14}15log.Panic(0xc0000a7f08, 0x1, 0x1)16main.main()17log.Panicf()18import (19func main() {20 log.Panicf("Error: %s", "Panic error")21}
Errorf
Using AI Code Generation
1import (2func main() {3 log.Printf("This is an error: %v", err)4}5Example 2: Using log.Fatalf() method6import (7func main() {8 log.Fatalf("This is an error: %v", err)9}10Example 3: Using log.Panicf() method11import (12func main() {13 log.Panicf("This is an error: %v", err)14}15log.(*Logger).Panicf(0xc0000a4000, 0x4a0c8e, 0x14, 0xc0000b7f60, 0x1, 0x1)16main.main()17Example 4: Using log.Panicln() method18import (19func main() {20 log.Panicln("This is an error:", err)21}22log.(*Logger).Panicln(0xc0000a4000, 0xc0000b7f90
Errorf
Using AI Code Generation
1import (2func main() {3 log.Printf("This is a log message")4}5import (6func main() {7 log.Panicf("This is a panic message")8}9log.Panicf(0x4b9d1a, 0x14, 0xc0000b3e78, 0x1, 0x1)10main.main()11import (12func main() {13 log.Fatalf("This is a fatal message")14}15log.Fatalf(0x4b9d1a, 0x14, 0xc0000b3e78, 0x1, 0x1)16main.main()17import (18func main() {19 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)20 log.Printf("This is a log message")21}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!