1N/A#
1N/A# Copyright 2005 Sun Microsystems, Inc. All rights reserved.
1N/A# Use is subject to license terms.
1N/A#
1N/A# CDDL HEADER START
1N/A#
1N/A# The contents of this file are subject to the terms of the
1N/A# Common Development and Distribution License, Version 1.0 only
1N/A# (the "License"). You may not use this file except in compliance
1N/A# with the License.
1N/A#
1N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1N/A# or http://www.opensolaris.org/os/licensing.
1N/A# See the License for the specific language governing permissions
1N/A# and limitations under the License.
1N/A#
1N/A# When distributing Covered Code, include this CDDL HEADER in each
1N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1N/A# If applicable, add the following below this CDDL HEADER, with the
1N/A# fields enclosed by brackets "[]" replaced with your own identifying
1N/A# information: Portions Copyright [yyyy] [name of copyright owner]
1N/A#
1N/A# CDDL HEADER END
1N/A#
1N/A#ident "%Z%%M% %I% %E% SMI"
1N/A#
1N/A# test script for Sun::Solaris::Kstat
1N/A#
1N/A
1N/Ause strict;
1N/A
1N/A# Visit all the leaf nodes -
1N/A# will generate a die if there are any structure mismatches
1N/Asub visit_all($)
1N/A{
1N/A my ($ks) = @_;
1N/A foreach my $m (sort(keys(%$ks))) {
1N/A foreach my $i (sort(keys(%{$ks->{$m}}))) {
1N/A foreach my $n (sort(keys(%{$ks->{$m}->{$i}}))) {
1N/A foreach my $k (sort(
1N/A keys(%{$ks->{$m}->{$i}->{$n}}))) {
1N/A my $v = $ks->{$m}->{$i}->{$n}->{$k};
1N/A }
1N/A }
1N/A }
1N/A }
1N/A return(1);
1N/A}
1N/A
1N/ABEGIN { $| = 1; print "1..15\n"; }
1N/Amy $loaded;
1N/AEND {print "not ok 1\n" unless $loaded;}
1N/Ause Sun::Solaris::Kstat;
1N/A$loaded = 1;
1N/Aprint "ok 1\n";
1N/A
1N/A# Check we can create a Kstat object OK
1N/Amy ($test, $ks);
1N/A$test = 2;
1N/Aif (! eval { $ks = Sun::Solaris::Kstat->new() }) {
1N/A print("not ok $test: $@");
1N/A} else {
1N/A print("ok $test\n");
1N/A}
1N/A$test++;
1N/A
1N/A# Check FIRSTKEY/NEXTKEY/FETCH and for structure mismatches
1N/Aif (! eval { visit_all($ks) }) {
1N/A print("not ok $test: $@");
1N/A} else {
1N/A print("ok $test\n");
1N/A}
1N/A$test++;
1N/A
1N/A# Find a cpu number
1N/Amy $cpu = (keys(%{$ks->{cpu_info}}))[0];
1N/Amy $cpu_info = "cpu_info$cpu";
1N/A
1N/A# Check EXISTS
1N/Aif (exists($ks->{cpu_info}{$cpu}{$cpu_info}{state})) {
1N/A print("ok $test\n");
1N/A} else {
1N/A print("not ok $test\n");
1N/A}
1N/A$test++;
1N/A
1N/A# Check DELETE
1N/Amy $val = delete($ks->{cpu_info}{$cpu}{$cpu_info}{state});
1N/Aif (defined($val) && ($val =~ /^on-line/ || $val =~ /^off-line/)) {
1N/A print("ok $test\n");
1N/A} else {
1N/A print("not ok $test ($val)\n");
1N/A}
1N/A$test++;
1N/A
1N/A# 5.004_04 has a broken hv_delete
1N/Aif ($] < 5.00405) {
1N/A print("ok $test\n");
1N/A $test++;
1N/A print("ok $test\n");
1N/A $test++;
1N/A} else {
1N/A if (! exists($ks->{cpu_info}{$cpu}{$cpu_info}{state})) {
1N/A print("ok $test\n");
1N/A } else {
1N/A print("not ok $test\n");
1N/A }
1N/A $test++;
1N/A $val = $ks->{cpu_info}{$cpu}{$cpu_info}{state};
1N/A if (! defined($val)) {
1N/A print("ok $test\n");
1N/A } else {
1N/A print("not ok $test\n");
1N/A }
1N/A $test++;
1N/A}
1N/A
1N/A# Check STORE
1N/A$ks->{cpu_info}{$cpu}{$cpu_info}{state} = "california";
1N/Aif ($ks->{cpu_info}{$cpu}{$cpu_info}{state} eq "california") {
1N/A print("ok $test\n");
1N/A} else {
1N/A print("not ok $test\n");
1N/A}
1N/A$test++;
1N/A
1N/A# Check CLEAR
1N/Amy @bvals = sort(keys(%{$ks->{cpu_info}{$cpu}{$cpu_info}}));
1N/A%{$ks->{cpu_info}{$cpu}{$cpu_info}} = ();
1N/Amy @avals = sort(keys(%{$ks->{cpu_info}{$cpu}{$cpu_info}}));
1N/Awhile (@bvals || @avals) {
1N/A my $a = shift(@avals);
1N/A my $b = shift(@bvals);
1N/A if ($a ne $b) { print("not ok $test ($a ne $b)\n"); last; }
1N/A}
1N/Aprint("ok $test\n") if (! @avals && ! @bvals);
1N/A$test++;
1N/A
1N/A# Check updates
1N/Aif (! defined(eval { $ks->update() })) {
1N/A print("not ok $test: $@");
1N/A} else {
1N/A print("ok $test\n");
1N/A}
1N/A$test++;
1N/A
1N/A# Check readonly-ness of hash structure
1N/Aeval { $ks->{cpu_info}{$cpu}{$cpu_info} = {}; };
1N/Aprint($@ =~ /^Modification of a read-only/i ? "ok $test\n" : "not ok $test\n");
1N/A$test++;
1N/A
1N/Aeval { $ks->{cpu_info}{$cpu} = {}; };
1N/Aprint($@ =~ /^Modification of a read-only/i ? "ok $test\n" : "not ok $test\n");
1N/A$test++;
1N/A
1N/Aeval { $ks->{cpu_info} = {}; };
1N/Aprint($@ =~ /^Modification of a read-only/i ? "ok $test\n" : "not ok $test\n");
1N/A$test++;
1N/A
1N/A# Check timestamps
1N/Amy $then = $ks->{cpu_info}{$cpu}{$cpu_info}{snaptime};
1N/Asleep(3);
1N/Aif (! defined(eval { $ks->update() })) {
1N/A print("not ok $test: $@");
1N/A} else {
1N/A print("ok $test\n");
1N/A}
1N/A$test++;
1N/Amy $interval = $ks->{cpu_info}{$cpu}{$cpu_info}{snaptime} - $then;
1N/Aif ($interval >= 2.5 && $interval <= 3.5) {
1N/A print("ok $test\n");
1N/A} else {
1N/A print("not ok $test\n");
1N/A}
1N/A$test++;
1N/A
1N/Aexit(0);