xref: /illumos-gate/usr/src/cmd/perl/contrib/Sun/Solaris/Kstat/t/Kstat.t (revision 581cede61ac9c14d8d4ea452562a567189eead78)
1#
2# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3# Use is subject to license terms.
4#
5# CDDL HEADER START
6#
7# The contents of this file are subject to the terms of the
8# Common Development and Distribution License, Version 1.0 only
9# (the "License").  You may not use this file except in compliance
10# with the License.
11#
12# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
13# or http://www.opensolaris.org/os/licensing.
14# See the License for the specific language governing permissions
15# and limitations under the License.
16#
17# When distributing Covered Code, include this CDDL HEADER in each
18# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
19# If applicable, add the following below this CDDL HEADER, with the
20# fields enclosed by brackets "[]" replaced with your own identifying
21# information: Portions Copyright [yyyy] [name of copyright owner]
22#
23# CDDL HEADER END
24#
25#ident	"%Z%%M%	%I%	%E% SMI"
26#
27# test script for Sun::Solaris::Kstat
28#
29
30use strict;
31
32# Visit all the leaf nodes -
33# will generate a die if there are any structure mismatches
34sub visit_all($)
35{
36	my ($ks) = @_;
37	foreach my $m (sort(keys(%$ks))) {
38		foreach my $i (sort(keys(%{$ks->{$m}}))) {
39			foreach my $n (sort(keys(%{$ks->{$m}->{$i}}))) {
40				foreach my $k (sort(
41				    keys(%{$ks->{$m}->{$i}->{$n}}))) {
42					my $v = $ks->{$m}->{$i}->{$n}->{$k};
43				}
44			}
45		}
46	}
47	return(1);
48}
49
50BEGIN { $| = 1; print "1..15\n"; }
51my $loaded;
52END {print "not ok 1\n" unless $loaded;}
53use Sun::Solaris::Kstat;
54$loaded = 1;
55print "ok 1\n";
56
57# Check we can create a Kstat object OK
58my ($test, $ks);
59$test = 2;
60if (! eval { $ks = Sun::Solaris::Kstat->new() }) {
61	print("not ok $test: $@");
62} else {
63	print("ok $test\n");
64}
65$test++;
66
67# Check FIRSTKEY/NEXTKEY/FETCH and for structure mismatches
68if (! eval { visit_all($ks) }) {
69	print("not ok $test: $@");
70} else {
71	print("ok $test\n");
72}
73$test++;
74
75# Find a cpu number
76my $cpu = (keys(%{$ks->{cpu_info}}))[0];
77my $cpu_info = "cpu_info$cpu";
78
79# Check EXISTS
80if (exists($ks->{cpu_info}{$cpu}{$cpu_info}{state})) {
81	print("ok $test\n");
82} else {
83	print("not ok $test\n");
84}
85$test++;
86
87# Check DELETE
88my $val = delete($ks->{cpu_info}{$cpu}{$cpu_info}{state});
89if (defined($val) && ($val =~ /^on-line/ || $val =~ /^off-line/)) {
90	print("ok $test\n");
91} else {
92	print("not ok $test ($val)\n");
93}
94$test++;
95
96# 5.004_04 has a broken hv_delete
97if ($] < 5.00405) {
98	print("ok $test\n");
99	$test++;
100	print("ok $test\n");
101	$test++;
102} else {
103	if (! exists($ks->{cpu_info}{$cpu}{$cpu_info}{state})) {
104		print("ok $test\n");
105	} else {
106		print("not ok $test\n");
107	}
108	$test++;
109	$val = $ks->{cpu_info}{$cpu}{$cpu_info}{state};
110	if (! defined($val)) {
111		print("ok $test\n");
112	} else {
113		print("not ok $test\n");
114	}
115	$test++;
116}
117
118# Check STORE
119$ks->{cpu_info}{$cpu}{$cpu_info}{state} = "california";
120if ($ks->{cpu_info}{$cpu}{$cpu_info}{state} eq "california") {
121	print("ok $test\n");
122} else {
123	print("not ok $test\n");
124}
125$test++;
126
127# Check CLEAR
128my @bvals = sort(keys(%{$ks->{cpu_info}{$cpu}{$cpu_info}}));
129%{$ks->{cpu_info}{$cpu}{$cpu_info}} = ();
130my @avals = sort(keys(%{$ks->{cpu_info}{$cpu}{$cpu_info}}));
131while (@bvals || @avals) {
132	my $a = shift(@avals);
133	my $b = shift(@bvals);
134	if ($a ne $b) { print("not ok $test ($a ne $b)\n"); last; }
135}
136print("ok $test\n") if (! @avals && ! @bvals);
137$test++;
138
139# Check updates
140if (! defined(eval { $ks->update() })) {
141	print("not ok $test: $@");
142} else {
143	print("ok $test\n");
144}
145$test++;
146
147# Check readonly-ness of hash structure
148eval { $ks->{cpu_info}{$cpu}{$cpu_info} = {}; };
149print($@ =~ /^Modification of a read-only/i ? "ok $test\n" : "not ok $test\n");
150$test++;
151
152eval { $ks->{cpu_info}{$cpu} = {}; };
153print($@ =~ /^Modification of a read-only/i ? "ok $test\n" : "not ok $test\n");
154$test++;
155
156eval { $ks->{cpu_info} = {}; };
157print($@ =~ /^Modification of a read-only/i ? "ok $test\n" : "not ok $test\n");
158$test++;
159
160# Check timestamps
161my $then = $ks->{cpu_info}{$cpu}{$cpu_info}{snaptime};
162sleep(3);
163if (! defined(eval { $ks->update() })) {
164	print("not ok $test: $@");
165} else {
166	print("ok $test\n");
167}
168$test++;
169my $interval = $ks->{cpu_info}{$cpu}{$cpu_info}{snaptime} - $then;
170if ($interval >= 2.5 && $interval <= 3.5) {
171	print("ok $test\n");
172} else {
173	print("not ok $test\n");
174}
175$test++;
176
177exit(0);
178